Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::vm::profile_data Struct Reference

#include <profile.hpp>

Collaboration diagram for sysio::vm::profile_data:

Classes

struct  item
 
struct  list_header
 

Public Member Functions

template<typename Backend >
 profile_data (const std::string &file, Backend &bkend, const std::size_t buffer_size=65536, std::size_t hash_table_size=1024)
 
 ~profile_data ()
 
void write_header ()
 
void write_trailer ()
 
void flush ()
 
void write (const char *data, std::size_t size)
 
void write (const item *item)
 
itemevict_oldest ()
 
void move_to_head (list_header *new_head)
 
void insert_hash (item *new_item)
 
void flush_hash ()
 
void handle_tick (void **data, int count)
 
template<typename Context >
void init_backtrace (const Context &context)
 
template<typename Backend >
 profile_data (const std::string &file, Backend &bkend, const std::size_t buffer_size=65536, std::size_t hash_table_size=1024)
 
 ~profile_data ()
 
void write_header ()
 
void write_trailer ()
 
void flush ()
 
void write (const char *data, std::size_t size)
 
void write (const item *item)
 
itemevict_oldest ()
 
void move_to_head (list_header *new_head)
 
void insert_hash (item *new_item)
 
void flush_hash ()
 
void handle_tick (void **data, int count)
 
template<typename Context >
void init_backtrace (const Context &context)
 

Static Public Member Functions

static bool traces_equal (const item *lhs, const item *rhs)
 
static bool traces_equal (const item *lhs, const item *rhs)
 

Public Attributes

int fd
 
char * outbuf
 
std::size_t outpos
 
std::size_t outsize
 
list_header mru_list
 
item ** table
 
std::size_t table_size
 
const profile_instr_mapaddr_map
 
int(* get_backtrace_fn )(const void *, void **, int, void *)
 
const void * exec_context
 
std::vector< char > outbuf_storage
 
std::vector< itemitems_storage
 
std::vector< item * > table_storage
 

Static Public Attributes

static constexpr std::size_t max_frames = 251
 

Detailed Description

Definition at line 20 of file profile.hpp.

Constructor & Destructor Documentation

◆ profile_data() [1/2]

template<typename Backend >
sysio::vm::profile_data::profile_data ( const std::string & file,
Backend & bkend,
const std::size_t buffer_size = 65536,
std::size_t hash_table_size = 1024 )
inline

Definition at line 24 of file profile.hpp.

24 :
26 outbuf_storage(buffer_size),
27 items_storage(hash_table_size),
28 table_storage(hash_table_size) // load factor of 1
29 {
31
32 outbuf = outbuf_storage.data();
33 outpos = 0;
34 outsize = outbuf_storage.size();
35
36 list_header* prev = &mru_list;
37 for(auto& item : items_storage) {
38 item.mru.prev = prev;
39 item.mru.next = reinterpret_cast<list_header*>(&item + 1); // Avoid undefined behavior at the end
40 prev = &item.mru;
41 }
42 items_storage.back().mru.next = &mru_list;
43 mru_list.next = &items_storage.front().mru;
44 mru_list.prev = &items_storage.back().mru;
45
46 table = table_storage.data();
48
49 fd = open(file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0755);
51 }
const DebugInfo & get_debug() const
Definition backend.hpp:252
auto & get_context()
Definition backend.hpp:250
backend_t bkend(hello_wasm, ehm, &wa)
void init_backtrace(const Context &context)
Definition profile.hpp:241
const profile_instr_map & addr_map
Definition profile.hpp:236
std::vector< item > items_storage
Definition profile.hpp:250
std::vector< item * > table_storage
Definition profile.hpp:251
std::vector< char > outbuf_storage
Definition profile.hpp:249
Here is the call graph for this function:

◆ ~profile_data() [1/2]

sysio::vm::profile_data::~profile_data ( )
inline

Definition at line 52 of file profile.hpp.

52 {
53 flush_hash();
55 flush();
56 close(fd);
57 }
void close(T *e, websocketpp::connection_hdl hdl)
Here is the call graph for this function:

◆ profile_data() [2/2]

template<typename Backend >
sysio::vm::profile_data::profile_data ( const std::string & file,
Backend & bkend,
const std::size_t buffer_size = 65536,
std::size_t hash_table_size = 1024 )
inline

Definition at line 24 of file profile.hpp.

24 :
26 outbuf_storage(buffer_size),
27 items_storage(hash_table_size),
28 table_storage(hash_table_size) // load factor of 1
29 {
31
32 outbuf = outbuf_storage.data();
33 outpos = 0;
34 outsize = outbuf_storage.size();
35
36 list_header* prev = &mru_list;
37 for(auto& item : items_storage) {
38 item.mru.prev = prev;
39 item.mru.next = reinterpret_cast<list_header*>(&item + 1); // Avoid undefined behavior at the end
40 prev = &item.mru;
41 }
42 items_storage.back().mru.next = &mru_list;
43 mru_list.next = &items_storage.front().mru;
44 mru_list.prev = &items_storage.back().mru;
45
46 table = table_storage.data();
48
49 fd = open(file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0755);
51 }
Here is the call graph for this function:

◆ ~profile_data() [2/2]

sysio::vm::profile_data::~profile_data ( )
inline

Definition at line 52 of file profile.hpp.

52 {
53 flush_hash();
55 flush();
56 close(fd);
57 }
Here is the call graph for this function:

Member Function Documentation

◆ evict_oldest() [1/2]

item * sysio::vm::profile_data::evict_oldest ( )
inline

Definition at line 149 of file profile.hpp.

149 {
150 item* result = reinterpret_cast<item*>(mru_list.prev);
151 if(result->bucket != 0xFFFFFFFFu) {
152 write(result);
153 for(item** entry = &table[result->bucket]; ; entry = &(*entry)->next) {
154 if(*entry == result) {
155 *entry = result->next;
156 break;
157 }
158 }
159 result->bucket = 0xFFFFFFFFu;
160 }
161 return result;
162 }
void write(const char *data, std::size_t size)
Definition profile.hpp:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ evict_oldest() [2/2]

item * sysio::vm::profile_data::evict_oldest ( )
inline

Definition at line 149 of file profile.hpp.

149 {
150 item* result = reinterpret_cast<item*>(mru_list.prev);
151 if(result->bucket != 0xFFFFFFFFu) {
152 write(result);
153 for(item** entry = &table[result->bucket]; ; entry = &(*entry)->next) {
154 if(*entry == result) {
155 *entry = result->next;
156 break;
157 }
158 }
159 result->bucket = 0xFFFFFFFFu;
160 }
161 return result;
162 }
Here is the call graph for this function:

◆ flush() [1/2]

void sysio::vm::profile_data::flush ( )
inline

Definition at line 72 of file profile.hpp.

72 {
73 for(std::size_t i = 0; i < outpos;) {
74 auto res = ::write(fd, outbuf + i, outpos - i);
75 if(res == -1) {
76 // report_error
77 break;
78 } else {
79 i += res;
80 }
81 }
82 outpos = 0;
83 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush() [2/2]

void sysio::vm::profile_data::flush ( )
inline

Definition at line 72 of file profile.hpp.

72 {
73 for(std::size_t i = 0; i < outpos;) {
74 auto res = ::write(fd, outbuf + i, outpos - i);
75 if(res == -1) {
76 // report_error
77 break;
78 } else {
79 i += res;
80 }
81 }
82 outpos = 0;
83 }
Here is the call graph for this function:

◆ flush_hash() [1/2]

void sysio::vm::profile_data::flush_hash ( )
inline

Definition at line 191 of file profile.hpp.

191 {
192 for(std::size_t i = 0; i < table_size; ++i) {
193 for(item* entry = table[i]; entry; entry = entry->next) {
194 write(entry);
195 entry->bucket = 0xFFFFFFFF;
196 }
197 table[i] = nullptr;
198 }
199 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush_hash() [2/2]

void sysio::vm::profile_data::flush_hash ( )
inline

Definition at line 191 of file profile.hpp.

191 {
192 for(std::size_t i = 0; i < table_size; ++i) {
193 for(item* entry = table[i]; entry; entry = entry->next) {
194 write(entry);
195 entry->bucket = 0xFFFFFFFF;
196 }
197 table[i] = nullptr;
198 }
199 }
Here is the call graph for this function:

◆ handle_tick() [1/2]

void sysio::vm::profile_data::handle_tick ( void ** data,
int count )
inline

Definition at line 202 of file profile.hpp.

202 {
203 item* entry = evict_oldest();
204
205 std::size_t out = 0;
206 // Translate addresses to wasm addresses; skip any frames that are outside wasm
207 for(int i = 0; i < count && out < max_frames; ++i) {
208 auto addr = addr_map.translate(data[i]);
209 if(addr != 0xFFFFFFFFu) {
210 if(out > 0) {
211 ++addr;
212 }
213 entry->frames[out++] = addr;
214 }
215 }
216 if(out != 0) {
217 entry->len = out;
218 if(out % 2 == 0) {
219 entry->frames[out] = 0; // so hashing can align to a 64-bit boundary
220 }
221 insert_hash(entry);
222 }
223 }
std::uint32_t translate(const void *pc) const
int * count
void insert_hash(item *new_item)
Definition profile.hpp:174
static constexpr std::size_t max_frames
Definition profile.hpp:93
Here is the call graph for this function:

◆ handle_tick() [2/2]

void sysio::vm::profile_data::handle_tick ( void ** data,
int count )
inline

Definition at line 202 of file profile.hpp.

202 {
203 item* entry = evict_oldest();
204
205 std::size_t out = 0;
206 // Translate addresses to wasm addresses; skip any frames that are outside wasm
207 for(int i = 0; i < count && out < max_frames; ++i) {
208 auto addr = addr_map.translate(data[i]);
209 if(addr != 0xFFFFFFFFu) {
210 if(out > 0) {
211 ++addr;
212 }
213 entry->frames[out++] = addr;
214 }
215 }
216 if(out != 0) {
217 entry->len = out;
218 if(out % 2 == 0) {
219 entry->frames[out] = 0; // so hashing can align to a 64-bit boundary
220 }
221 insert_hash(entry);
222 }
223 }
Here is the call graph for this function:

◆ init_backtrace() [1/2]

template<typename Context >
void sysio::vm::profile_data::init_backtrace ( const Context & context)
inline

Definition at line 241 of file profile.hpp.

241 {
242 get_backtrace_fn = [](const void* ctx, void** data, int len, void* uc) {
243 return static_cast<const Context*>(ctx)->backtrace(data, len, uc);
244 };
246 }
sysio::client::http::http_context context
Definition main.cpp:200
int(* get_backtrace_fn)(const void *, void **, int, void *)
Definition profile.hpp:237
const void * exec_context
Definition profile.hpp:238
size_t len
Here is the caller graph for this function:

◆ init_backtrace() [2/2]

template<typename Context >
void sysio::vm::profile_data::init_backtrace ( const Context & context)
inline

Definition at line 241 of file profile.hpp.

241 {
242 get_backtrace_fn = [](const void* ctx, void** data, int len, void* uc) {
243 return static_cast<const Context*>(ctx)->backtrace(data, len, uc);
244 };
246 }

◆ insert_hash() [1/2]

void sysio::vm::profile_data::insert_hash ( item * new_item)
inline

Definition at line 174 of file profile.hpp.

174 {
175 std::size_t hash = new_item->hash();
176 std::size_t idx = hash & (table_size - 1);
177 for(item* bucket_entry = table[idx]; bucket_entry; bucket_entry = bucket_entry->next) {
178 if(traces_equal(new_item, bucket_entry)) {
179 ++bucket_entry->count;
180 move_to_head(&bucket_entry->mru);
181 return;
182 }
183 }
184 new_item->next = table[idx];
185 new_item->bucket = idx;
186 new_item->count = 1;
187 table[idx] = new_item;
188 move_to_head(&new_item->mru);
189 }
void move_to_head(list_header *new_head)
Definition profile.hpp:164
static bool traces_equal(const item *lhs, const item *rhs)
Definition profile.hpp:133
Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert_hash() [2/2]

void sysio::vm::profile_data::insert_hash ( item * new_item)
inline

Definition at line 174 of file profile.hpp.

174 {
175 std::size_t hash = new_item->hash();
176 std::size_t idx = hash & (table_size - 1);
177 for(item* bucket_entry = table[idx]; bucket_entry; bucket_entry = bucket_entry->next) {
178 if(traces_equal(new_item, bucket_entry)) {
179 ++bucket_entry->count;
180 move_to_head(&bucket_entry->mru);
181 return;
182 }
183 }
184 new_item->next = table[idx];
185 new_item->bucket = idx;
186 new_item->count = 1;
187 table[idx] = new_item;
188 move_to_head(&new_item->mru);
189 }
Here is the call graph for this function:

◆ move_to_head() [1/2]

void sysio::vm::profile_data::move_to_head ( list_header * new_head)
inline

Definition at line 164 of file profile.hpp.

164 {
165 new_head->unlink();
166 new_head->next = mru_list.next;
167 new_head->prev = &mru_list;
168 mru_list.next->prev = new_head;
169 mru_list.next = new_head;
170 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ move_to_head() [2/2]

void sysio::vm::profile_data::move_to_head ( list_header * new_head)
inline

Definition at line 164 of file profile.hpp.

164 {
165 new_head->unlink();
166 new_head->next = mru_list.next;
167 new_head->prev = &mru_list;
168 mru_list.next->prev = new_head;
169 mru_list.next = new_head;
170 }
Here is the call graph for this function:

◆ traces_equal() [1/2]

static bool sysio::vm::profile_data::traces_equal ( const item * lhs,
const item * rhs )
inlinestatic

Definition at line 133 of file profile.hpp.

133 {
134 if(lhs->len != rhs->len) {
135 return false;
136 }
137 for(uint32_t i = 0; i < lhs->len; ++i) {
138 if(lhs->frames[i] != rhs->frames[i]) {
139 return false;
140 }
141 }
142 return true;
143 }
unsigned int uint32_t
Definition stdint.h:126
Here is the caller graph for this function:

◆ traces_equal() [2/2]

static bool sysio::vm::profile_data::traces_equal ( const item * lhs,
const item * rhs )
inlinestatic

Definition at line 133 of file profile.hpp.

133 {
134 if(lhs->len != rhs->len) {
135 return false;
136 }
137 for(uint32_t i = 0; i < lhs->len; ++i) {
138 if(lhs->frames[i] != rhs->frames[i]) {
139 return false;
140 }
141 }
142 return true;
143 }

◆ write() [1/4]

void sysio::vm::profile_data::write ( const char * data,
std::size_t size )
inline

Definition at line 85 of file profile.hpp.

85 {
86 if(size + outpos >= outsize) {
87 flush();
88 }
89 std::memcpy(outbuf + outpos, data, size);
90 outpos += size;
91 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write() [2/4]

void sysio::vm::profile_data::write ( const char * data,
std::size_t size )
inline

Definition at line 85 of file profile.hpp.

85 {
86 if(size + outpos >= outsize) {
87 flush();
88 }
89 std::memcpy(outbuf + outpos, data, size);
90 outpos += size;
91 }
Here is the call graph for this function:

◆ write() [3/4]

void sysio::vm::profile_data::write ( const item * item)
inline

Definition at line 145 of file profile.hpp.

145 {
146 write(reinterpret_cast<const char*>(&item->count), (2+item->len) * sizeof(std::uint32_t));
147 }
Here is the call graph for this function:

◆ write() [4/4]

void sysio::vm::profile_data::write ( const item * item)
inline

Definition at line 145 of file profile.hpp.

145 {
146 write(reinterpret_cast<const char*>(&item->count), (2+item->len) * sizeof(std::uint32_t));
147 }
Here is the call graph for this function:

◆ write_header() [1/2]

void sysio::vm::profile_data::write_header ( )
inline

Definition at line 58 of file profile.hpp.

58 {
59 uint32_t header[] = {
60 0,
61 3,
62 0,
64 0
65 };
66 write(reinterpret_cast<const char*>(header), sizeof(header));
67 }
uint32_t profile_interval_us
Definition profile.hpp:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_header() [2/2]

void sysio::vm::profile_data::write_header ( )
inline

Definition at line 58 of file profile.hpp.

58 {
59 uint32_t header[] = {
60 0,
61 3,
62 0,
64 0
65 };
66 write(reinterpret_cast<const char*>(header), sizeof(header));
67 }
Here is the call graph for this function:

◆ write_trailer() [1/2]

void sysio::vm::profile_data::write_trailer ( )
inline

Definition at line 68 of file profile.hpp.

68 {
69 uint32_t trailer[] = { 0, 1, 0 };
70 write(reinterpret_cast<const char*>(trailer), sizeof(trailer));
71 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_trailer() [2/2]

void sysio::vm::profile_data::write_trailer ( )
inline

Definition at line 68 of file profile.hpp.

68 {
69 uint32_t trailer[] = { 0, 1, 0 };
70 write(reinterpret_cast<const char*>(trailer), sizeof(trailer));
71 }
Here is the call graph for this function:

Member Data Documentation

◆ addr_map

const profile_instr_map & sysio::vm::profile_data::addr_map

Definition at line 236 of file profile.hpp.

◆ exec_context

const void * sysio::vm::profile_data::exec_context

Definition at line 238 of file profile.hpp.

◆ fd

int sysio::vm::profile_data::fd

Definition at line 225 of file profile.hpp.

◆ get_backtrace_fn

int(* sysio::vm::profile_data::get_backtrace_fn)(const void *, void **, int, void *)

Definition at line 237 of file profile.hpp.

◆ items_storage

std::vector< item > sysio::vm::profile_data::items_storage

Definition at line 250 of file profile.hpp.

◆ max_frames

static constexpr std::size_t sysio::vm::profile_data::max_frames = 251
staticconstexpr

Definition at line 93 of file profile.hpp.

◆ mru_list

list_header sysio::vm::profile_data::mru_list

Definition at line 231 of file profile.hpp.

◆ outbuf

char * sysio::vm::profile_data::outbuf

Definition at line 227 of file profile.hpp.

◆ outbuf_storage

std::vector< char > sysio::vm::profile_data::outbuf_storage

Definition at line 249 of file profile.hpp.

◆ outpos

std::size_t sysio::vm::profile_data::outpos

Definition at line 228 of file profile.hpp.

◆ outsize

std::size_t sysio::vm::profile_data::outsize

Definition at line 229 of file profile.hpp.

◆ table

item ** sysio::vm::profile_data::table

Definition at line 232 of file profile.hpp.

◆ table_size

std::size_t sysio::vm::profile_data::table_size

Definition at line 233 of file profile.hpp.

◆ table_storage

std::vector< item * > sysio::vm::profile_data::table_storage

Definition at line 251 of file profile.hpp.


The documentation for this struct was generated from the following files: