Scanner C++ API
profiler.h
1 /* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/util/util.h"
19 
20 #include <atomic>
21 #include <fstream>
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 namespace storehouse {
27 class WriteFile;
28 }
29 
30 namespace scanner {
31 
32 enum ProfilerLevel {
33  Debug = 0,
34  Info = 1,
35  Important = 2
36 };
37 
38 extern ProfilerLevel PROFILER_LEVEL;
39 
40 class Profiler {
41  public:
42  Profiler(timepoint_t base_time);
43 
44  Profiler(const Profiler& other);
45 
46  void add_interval(const std::string& key, timepoint_t start, timepoint_t end, ProfilerLevel level=ProfilerLevel::Info);
47 
48  void increment(const std::string& key, int64_t value);
49 
50  void reset(timepoint_t base_time);
51 
52  struct TaskRecord {
53  std::string key;
54  int64_t start;
55  int64_t end;
56  };
57 
58  const std::vector<TaskRecord>& get_records() const;
59 
60  const std::map<std::string, int64_t>& get_counters() const;
61 
62  protected:
63  void spin_lock();
64  void unlock();
65 
66  timepoint_t base_time_;
67  std::atomic_flag lock_;
68  std::vector<TaskRecord> records_;
69  std::map<std::string, int64_t> counters_;
70 };
71 
72 class ProfileBlock {
73  public:
74  ProfileBlock(Profiler* profiler, std::string label);
75  ~ProfileBlock();
76 protected:
77  Profiler* profiler_;
78  std::string label_;
79  timepoint_t start_;
80 };
81 
82 void write_profiler_to_file(storehouse::WriteFile* file, int64_t node,
83  std::string type_name, std::string tag,
84  int64_t worker_num, const Profiler& profiler);
85 
86 } // namespace scanner
87 
88 #include "scanner/util/profiler.inl"
Definition: profiler.h:26
Definition: profiler.h:40
Definition: profiler.h:52
Definition: database.cpp:36
Definition: profiler.h:72