1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_SYSTEM_MODEL_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_SYSTEM_MODEL_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/values.h"
12 #include "chrome/browser/chromeos/arc/tracing/arc_cpu_event.h"
13 #include "chrome/browser/chromeos/arc/tracing/arc_value_event.h"
14 
15 namespace arc {
16 
17 // Contains information about system activity and involved threads. System
18 // activity includes CPU and memory events.
19 class ArcSystemModel {
20  public:
21   static constexpr int kUnknownPid = -1;
22 
23   struct ThreadInfo {
24     ThreadInfo();
25     ThreadInfo(int pid, const std::string& name);
26 
27     bool operator==(const ThreadInfo& other) const;
28 
29     // Process id or |kUnknownPid| if unknown.
30     int pid = kUnknownPid;
31     // Name of thread of process in case thread is main thread of the process.
32     std::string name;
33   };
34 
35   using ThreadMap = std::map<int, ThreadInfo>;
36 
37   ArcSystemModel();
38   ~ArcSystemModel();
39 
40   void Reset();
41   // Trims the model using |trim_timestamp|. Events before
42   // |trim_timestamp| are consolidated with their timestamps aligned
43   // to |trim_timestamp|. Events on or after |trim_timestamp| are left
44   // in the model unchanged.
45   void Trim(uint64_t trim_timestamp);
46 
47   // Closes range for each value event type by extending the latest value till
48   // the |max_timestamp|.
49   void CloseRangeForValueEvents(uint64_t max_timestamp);
50 
51   void CopyFrom(const ArcSystemModel& other);
52   base::DictionaryValue Serialize() const;
53   bool Load(const base::Value* root);
54 
55   bool operator==(const ArcSystemModel& other) const;
56 
thread_map()57   ThreadMap& thread_map() { return thread_map_; }
thread_map()58   const ThreadMap& thread_map() const { return thread_map_; }
59 
all_cpu_events()60   AllCpuEvents& all_cpu_events() { return all_cpu_events_; }
all_cpu_events()61   const AllCpuEvents& all_cpu_events() const { return all_cpu_events_; }
62 
memory_events()63   ValueEvents& memory_events() { return memory_events_; }
memory_events()64   const ValueEvents& memory_events() const { return memory_events_; }
65 
66  private:
67   ThreadMap thread_map_;
68   AllCpuEvents all_cpu_events_;
69   // TODO(khmel): For simplification and performance use separate channels
70   // for each event type.
71   ValueEvents memory_events_;
72 
73   DISALLOW_COPY_AND_ASSIGN(ArcSystemModel);
74 };
75 
76 }  // namespace arc
77 
78 #endif  // CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_SYSTEM_MODEL_H_
79