btftoolchain
btf.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <any>
5 #include <fstream>
6 #include <iostream>
7 #include <list>
8 #include <sstream>
9 #include <string>
10 #include <string_view>
11 #include <unordered_map>
12 #include <unordered_set>
13 #include <vector>
14 
15 #include "btf_entity_types.h"
16 #include "btf_signal.h"
17 #include "common.h"
18 #include "core.h"
19 #include "os.h"
20 #include "process.h"
21 #include "runnable.h"
22 #include "scheduler.h"
23 #include "semaphore.h"
24 #include "simulation.h"
25 #include "stimulus.h"
26 
27 // Supported Entities
28 // - Cores (only idle, execution and frequency change)
29 // - OS
30 // - Tasks/ISRs (Process)
31 // - Runnables
32 // - Scheduler
33 // - Signals
34 // - Stimuli
35 
36 // Not supported general
37 // - Entity Id Tables
38 
39 // FIXME propably better to make the storage of events a vector (not a list), because faster and random accessable
40 // requires to drop support for instertEvent
41 
42 namespace btf
43 {
44 
48 struct PairHash
49 {
50  template <typename T1, typename T2> std::size_t operator()(const std::pair<T1, T2>& p) const
51  {
52  auto hash1 = std::hash<T1>{}(p.first);
53  auto hash2 = std::hash<T1>{}(p.second);
54 
55  return (hash1 << 1) + hash1 + hash2;
56  }
57 };
58 
62 struct TupleHash
63 {
64  template <typename T1, typename T2, typename T3> std::size_t operator()(const std::tuple<T1, T2, T3>& t) const
65  {
66  return std::get<0>(t)
67  ^ std::get<1>(t)
68  ^ std::get<2>(t);
69  }
70 };
71 
76 {
77  template <typename T1, typename T2, typename T3, typename T4> std::size_t operator()(const std::pair<std::tuple<T1, T2, T3>,T4>& q) const
78  {
79  return std::get<0>(q.first)
80  ^ std::get<1>(q.first)
81  ^ std::get<2>(q.first)
82  ^ q.second;
83  }
84 };
85 
91 class BtfEntry
92 {
93  public:
94 
102  union Events {
112 
115 
119 
123 
127 
131 
135 
139 
143 
147 
151  };
152 
154  uint64_t time_{0};
155 
158 
160  size_t source_hash_{0};
162  uint64_t source_instance_{0};
164  size_t target_hash_{0};
166  uint64_t target_instance_{0};
167 
170 
172  std::string note_;
173 
174 
180  std::string toString(std::unordered_map<size_t, std::string>& hash_map) const;
181 
186  std::string eventToString() const;
187 };
188 
193 class BtfFile
194 {
195  public:
196 
200  enum class TimeScales
201  {
203  pico_seconds,
205  nano_seconds,
210  };
211 
235  explicit BtfFile(std::string path, TimeScales time_scale = TimeScales::nano_seconds, bool auto_suspend_parent_runnable = true,
236  bool source_is_core = true, bool auto_generate_core_events = false, bool auto_wait_resume_os_events = false);
237 
241  void finish();
242 
248  void importFromFile(const std::string& path, char delimiter = ',');
249 
255  void setStringHashMap(std::unordered_map<size_t, std::string> hash_map);
256 
260  ErrorCodes coreEvent(uint64_t time, const std::string& core, Core::Events core_event);
261 
273  ErrorCodes coreEvent(uint64_t time, size_t core_hash, Core::Events core_event);
274 
278  ErrorCodes osEvent(uint64_t time, const std::string& process, const std::string& os, OS::Events os_event);
279 
292  ErrorCodes osEvent(uint64_t time, size_t core_hash, size_t os_hash, OS::Events os_event);
293 
294 
298  ErrorCodes taskMigrationEvent(uint64_t time, const std::string& source_core, const std::string& destination_core, const std::string& task,
299  uint64_t task_instance_id);
300 
313  ErrorCodes taskMigrationEvent(uint64_t time, size_t source_core_hash, size_t destination_core_hash, size_t task_hash, uint64_t task_instance_id);
314 
318  ErrorCodes processEvent(uint64_t time, const std::string& source, const std::string& process, uint64_t process_instance_id, Process::Events process_event,
319  bool is_isr = false);
320 
339  ErrorCodes processEvent(uint64_t time, size_t source_hash, size_t process_hash, uint64_t process_instance_id, Process::Events process_event, bool is_isr = false);
340 
344  ErrorCodes runnableEvent(uint64_t time, const std::string& source, const std::string& runnable, Runnable::Events runnable_event);
345 
362  ErrorCodes runnableEvent(uint64_t time, size_t core_hash, size_t process_hash, size_t runnable_hash, Runnable::Events runnable_event);
363 
367  ErrorCodes semaphoreEvent(uint64_t time, const std::string& source, const std::string& target, Semaphore::Events semaphore_event, uint64_t note);
368 
383  ErrorCodes semaphoreEvent(uint64_t time, size_t semaphore_hash, Semaphore::Events semaphore_event, uint64_t note);
384 
401  ErrorCodes semaphoreEvent(uint64_t time, size_t core_hash, size_t semaphore_hash, Semaphore::Events semaphore_event, uint64_t note);
402 
403 
407  ErrorCodes schedulerEvent(uint64_t time, const std::string& source, const std::string& scheduler, Scheduler::Events scheduler_event);
408 
421  ErrorCodes schedulerEvent(uint64_t time, size_t core_hash, size_t scheduler_hash, Scheduler::Events scheduler_event);
422 
434  ErrorCodes schedulerEvent(uint64_t time, size_t scheduler_hash, Scheduler::Events scheduler_event);
435 
439  ErrorCodes signalEvent(uint64_t time, const std::string& source, const std::string& signal, Signal::Events signal_event,
440  const std::string& signal_value = "");
441 
454  ErrorCodes signalEvent(uint64_t time, size_t core_hash, size_t signal_hash, Signal::Events signal_event, const std::string& signal_value = "");
455 
459  ErrorCodes stimulusEvent(uint64_t time, const std::string& source, const std::string& target, Stimulus::Events stimulus_event);
460 
471  ErrorCodes stimulusEvent(uint64_t time, size_t stimulus_hash, Stimulus::Events stimulus_event);
472 
473 
474  std::list<std::list<BtfEntry>::iterator> getEventsForEntity(const std::string& entity);
475  std::list<std::list<BtfEntry>::iterator> getEventsForEntity(size_t entity_hash);
476 
481  void comment(const std::string& comment);
482 
487  void headerEntry(const std::string& header_entry);
488 
496  void insertEvent(std::list<std::list<BtfEntry>::iterator>::iterator pos, const BtfEntry& entry);
497 
503  std::list<std::list<BtfEntry>::iterator>& getEntityEvents(const std::string& entity);
504 
509  std::list<BtfEntry> getAllEvents() const;
510 
515  size_t getNumberOfAllEvents() const;
516 
520  ErrorCodes simulationEventProcessName(uint64_t time, const std::string& process, const std::string& name);
521 
528  ErrorCodes simulationEventProcessName(uint64_t time, size_t process_hash, const std::string& name);
529 
533  ErrorCodes simulationEventProcessCreation(uint64_t time, const std::string& process, uint64_t pid, uint64_t ppid);
534 
542  ErrorCodes simulationEventProcessCreation(uint64_t time, size_t process_hash, uint64_t pid, uint64_t ppid);
543 
547  ErrorCodes simulationEventThreadName(uint64_t time, const std::string& thread, const std::string& name);
548 
555  ErrorCodes simulationEventThreadName(uint64_t time, size_t thread_hash, const std::string& name);
556 
560  ErrorCodes simulationEventThreadCreation(uint64_t time, const std::string& thread, uint64_t tid, uint64_t pid);
561 
569  ErrorCodes simulationEventThreadCreation(uint64_t time, size_t thread_hash, uint64_t tid, uint64_t pid);
570 
575  void setIgnoreMultipleTaskReleases(bool value);
576 
577  private:
579  BtfFile(BtfFile&) = delete;
581  BtfFile(BtfFile&&) = delete;
582 
584  void operator=(BtfFile&) = delete;
586  void operator=(BtfFile&&) = delete;
587 
593  void generateCoreIdleEvent(uint64_t time, size_t source_hash);
594 
601  void generateCoreExecuteEvent(uint64_t time, size_t source_hash, Process::Events process_event);
602 
608  ErrorCodes checkTime(uint64_t time);
609 
616  ErrorCodes checkType(size_t hash, EntityTypes should_be_type);
617 
622  std::string getHeader() const;
623 
624 
625 
627  std::string path_;
628 
630  TimeScales time_scale_;
631 
633  uint64_t last_time_{0};
634 
636  std::unordered_map<size_t, Core> cores_;
637 
639  std::unordered_map<std::pair<size_t, uint64_t>, Process, PairHash> tasks_;
640 
642  std::unordered_map<std::pair<size_t, size_t>, Runnable, PairHash> runnables_;
643 
645  std::unordered_map<size_t, Semaphore> semaphores_;
646 
648  std::unordered_map<std::string, std::string> task_core_map_;
649 
651  std::unordered_map<std::pair<size_t, uint64_t>, std::vector<std::pair<size_t, uint64_t>>, PairHash> runnable_stacks_;
652 
654  std::unordered_map<std::pair<std::tuple<size_t, uint64_t, size_t>, size_t>, bool, QuadrupleHash> os_iswait_;
655 
657  std::unordered_map<size_t, std::string> hash_map_;
658 
660  std::list<BtfEntry> btf_entries_;
661 
663  std::unordered_map<size_t, std::list<std::list<BtfEntry>::iterator>> btf_entries_per_entity_;
664 
666  std::unordered_map<size_t, EntityTypes> type_map_;
667 
669  std::unordered_map<size_t, std::pair<size_t, size_t>> current_running_tasks_;
670 
672  const std::pair<size_t, size_t> no_running_task_{0, 0};
673 
675  std::unordered_map<size_t, bool> did_de_allocated_task_event_occurred_on_core_;
676 
678  std::unordered_map<size_t, uint64_t> stimuli_instance_ids_map_;
679 
681  std::unordered_map<size_t, std::vector<std::list<BtfEntry>::iterator>> runnable_without_task_buffers_;
682 
684  std::unordered_map<size_t, std::vector<std::pair<size_t, uint64_t>>> runnable_without_task_stacks_;
685 
687  std::unordered_map<size_t, uint64_t> runnable_instance_id_counters_;
688 
690  std::vector<std::string> custom_header_entries_;
691 
693  std::unordered_map<size_t, bool> did_task_allocation_event_happen_on_core_;
694 
696  bool auto_suspend_parent_runnable_;
697 
699  bool source_is_core_;
700 
702  bool auto_generate_core_events_;
703 
705  bool auto_wait_resume_os_events_;
706 
708  bool auto_generate_events_{true};
709 
711  bool ignore_multiple_task_releases_{ false };
712 };
713 } // namespace btf
Class for a BtfEntry.
Definition: btf.h:92
std::string eventToString() const
Converts union Events into string according to the type_ of the BtfEntry.
Definition: btf.cpp:54
Events event_
Events type of the BtfEntry.
Definition: btf.h:169
size_t target_hash_
Unique hash value of the target.
Definition: btf.h:164
uint64_t source_instance_
Source instance that triggered the event.
Definition: btf.h:162
EntityTypes type_
Entity type of the BtfEntry. The default value is "unknown".
Definition: btf.h:157
uint64_t target_instance_
Target instance of the event.
Definition: btf.h:166
std::string toString(std::unordered_map< size_t, std::string > &hash_map) const
Converts a BtfEntry into string.
Definition: btf.cpp:11
uint64_t time_
Timestamp of the BtfEntry.
Definition: btf.h:154
size_t source_hash_
Unique hash value of the source.
Definition: btf.h:160
std::string note_
Comments added to the BtfEntry.
Definition: btf.h:172
Main class of the btf lib. It contains all necessary functions to import and export a BTF file.
Definition: btf.h:194
BtfFile(std::string path, TimeScales time_scale=TimeScales::nano_seconds, bool auto_suspend_parent_runnable=true, bool source_is_core=true, bool auto_generate_core_events=false, bool auto_wait_resume_os_events=false)
Constructs a BTF file in memory.
Definition: btf.cpp:84
TimeScales
Enum class which contains the possible time scales (pico-, nano-, micro- or milliseconds).
Definition: btf.h:201
ErrorCodes runnableEvent(uint64_t time, const std::string &source, const std::string &runnable, Runnable::Events runnable_event)
Emits a runnable event (source is a core or process depending on the value of the core_is_source para...
Definition: btf.cpp:912
ErrorCodes processEvent(uint64_t time, const std::string &source, const std::string &process, uint64_t process_instance_id, Process::Events process_event, bool is_isr=false)
Emits a task event (except migration). See overloaded function for more information.
Definition: btf.cpp:630
ErrorCodes simulationEventThreadCreation(uint64_t time, const std::string &thread, uint64_t tid, uint64_t pid)
Emits a thread creation event. See overloaded function for more information.
Definition: btf.cpp:2029
ErrorCodes osEvent(uint64_t time, const std::string &process, const std::string &os, OS::Events os_event)
Emits an OS event (source is a core or a process depending on the value of the core_is_source paramet...
Definition: btf.cpp:448
ErrorCodes taskMigrationEvent(uint64_t time, const std::string &source_core, const std::string &destination_core, const std::string &task, uint64_t task_instance_id)
Emits a task migration event. See overloaded function for more information.
Definition: btf.cpp:548
std::list< std::list< BtfEntry >::iterator > getEventsForEntity(const std::string &entity)
Definition: btf.cpp:1792
void headerEntry(const std::string &header_entry)
Writes a custom entry to the header.
Definition: btf.cpp:1759
ErrorCodes simulationEventProcessCreation(uint64_t time, const std::string &process, uint64_t pid, uint64_t ppid)
Emits a process creation event. See overloaded function for more information.
Definition: btf.cpp:1947
ErrorCodes simulationEventProcessName(uint64_t time, const std::string &process, const std::string &name)
Emits a process name event (e.g. name change or initial name). See overloaded function for more infor...
Definition: btf.cpp:1906
std::list< BtfEntry > getAllEvents() const
Gets a list of all events.
Definition: btf.cpp:1896
ErrorCodes coreEvent(uint64_t time, const std::string &core, Core::Events core_event)
Emits a core event. See overloaded function for more information.
Definition: btf.cpp:399
void setIgnoreMultipleTaskReleases(bool value)
Sets the option to ignore multiple releases on a waiting task.
Definition: btf.cpp:2070
ErrorCodes stimulusEvent(uint64_t time, const std::string &source, const std::string &target, Stimulus::Events stimulus_event)
Emits a stimulus event. See overloaded function for more information.
Definition: btf.cpp:1695
ErrorCodes signalEvent(uint64_t time, const std::string &source, const std::string &signal, Signal::Events signal_event, const std::string &signal_value="")
Emits a signal event (source is a core or process depending on the value of the core_is_source parame...
Definition: btf.cpp:1610
void setStringHashMap(std::unordered_map< size_t, std::string > hash_map)
Sets the ID to name translation map. This should only be used for traces that use ID based APIs (e....
Definition: btf.cpp:394
void comment(const std::string &comment)
Emits a comment.
Definition: btf.cpp:1746
void finish()
Writes all BtfEntry instances to file.
Definition: btf.cpp:364
void insertEvent(std::list< std::list< BtfEntry >::iterator >::iterator pos, const BtfEntry &entry)
Inserts an event into the BTF trace. Warning: Use on own risk. No checks are performed and the inse...
Definition: btf.cpp:1780
size_t getNumberOfAllEvents() const
Gets the number of all events.
Definition: btf.cpp:1901
ErrorCodes semaphoreEvent(uint64_t time, const std::string &source, const std::string &target, Semaphore::Events semaphore_event, uint64_t note)
Emits a semaphore event. See overloaded function for more information.
Definition: btf.cpp:1419
void importFromFile(const std::string &path, char delimiter=',')
Appends the data from a BTF file. Currently only import into an empty BTF file is supported.
Definition: btf.cpp:91
ErrorCodes schedulerEvent(uint64_t time, const std::string &source, const std::string &scheduler, Scheduler::Events scheduler_event)
Emits a scheduler event. See overloaded function for more information.
Definition: btf.cpp:1291
ErrorCodes simulationEventThreadName(uint64_t time, const std::string &thread, const std::string &name)
Emits a thread name event (e.g. name change or initial name). See overloaded function for more inform...
Definition: btf.cpp:1989
std::list< std::list< BtfEntry >::iterator > & getEntityEvents(const std::string &entity)
Gets the list of events for a entity.
Definition: btf.cpp:1786
Class for the core (C) events.
Definition: core.h:14
Events
Possible types of core events.
Definition: core.h:36
Events
Possible types of OS events.
Definition: os.h:24
Events
Possible types of Process events.
Definition: process.h:22
Events
Possible types of runnable events.
Definition: runnable.h:43
Events
Possible types of Scheduler events.
Definition: scheduler.h:23
Events
Possible types of Semaphore events.
Definition: semaphore.h:25
Events
Possible types of Signal events.
Definition: btf_signal.h:26
Events
Possible types of simulation events.
Definition: simulation.h:22
Events
Possible types of Stimulus events.
Definition: stimulus.h:23
Definition: btf.h:43
EntityTypes
All types of entities that are supported by the btf lib.
Definition: btf_entity_types.h:13
@ unknown
default value
ErrorCodes
Error Codes for the btf library.
Definition: common.h:13
This struct generates a hash out of two values (uses a pair).
Definition: btf.h:49
std::size_t operator()(const std::pair< T1, T2 > &p) const
Definition: btf.h:50
This struct generates a hash out of four values (uses a pair where the first parameter is a tuple).
Definition: btf.h:76
std::size_t operator()(const std::pair< std::tuple< T1, T2, T3 >, T4 > &q) const
Definition: btf.h:77
This struct generates a hash out of three values (uses a tuple).
Definition: btf.h:63
std::size_t operator()(const std::tuple< T1, T2, T3 > &t) const
Definition: btf.h:64
Union Class for all supported Events.
Definition: btf.h:102
OS::Events os_event
Definition: btf.h:104
Core::Events core_event
Definition: btf.h:103
Events(Runnable::Events re)
Constructor for a BtfEntry with a Runnable event.
Definition: btf.h:130
Events()
Default Constructor which is assumed to be a BtfEntry with a Core event.
Definition: btf.h:114
Semaphore::Events semaphore_event
Definition: btf.h:108
Stimulus::Events stimulus_event
Definition: btf.h:111
Events(Simulation::Events se)
Constructor for a BtfEntry with a Simulation event.
Definition: btf.h:150
Scheduler::Events scheduler_event
Definition: btf.h:107
Simulation::Events simulation_event
Definition: btf.h:110
Process::Events process_event
Definition: btf.h:105
Events(Signal::Events se)
Constructor for a BtfEntry with a Signal event.
Definition: btf.h:142
Signal::Events signal_event
Definition: btf.h:109
Events(Semaphore::Events sem)
Constructor for a BtfEntry with a Semaphore event.
Definition: btf.h:138
Events(Stimulus::Events ste)
Constructor for a BtfEntry with a Stimulus event.
Definition: btf.h:146
Events(Scheduler::Events sche)
Constructor for a BtfEntry with a Scheduler event.
Definition: btf.h:134
Events(Core::Events ce)
Constructor for a BtfEntry with a Core event.
Definition: btf.h:118
Events(Process::Events te)
Constructor for a BtfEntry with a Process event.
Definition: btf.h:126
Events(OS::Events oe)
Constructor for a BtfEntry with an OS event.
Definition: btf.h:122
Runnable::Events runnable_event
Definition: btf.h:106