55
66#include " PyPerfNativeStackTrace.h"
77
8- #include < sys/uio .h>
8+ #include < cxxabi .h>
99#include < errno.h>
10+ #include < sys/uio.h>
11+ #include < time.h>
1012#include < unistd.h>
11- #include < cxxabi.h >
13+ #include < chrono >
1214#include < cstdio>
1315#include < cstring>
1416#include < sstream>
15- #include < time.h>
16- #include < chrono>
1717#include < thread>
1818
1919#include " PyPerfLoggingHelper.h"
@@ -31,8 +31,6 @@ uintptr_t NativeStackTrace::ip = 0;
3131time_t NativeStackTrace::now;
3232UnwindCache NativeStackTrace::cache;
3333
34-
35-
3634NativeStackTrace::NativeStackTrace (uint32_t pid, const unsigned char *raw_stack,
3735 size_t stack_len, uintptr_t ip, uintptr_t sp) : error_occurred(false ) {
3836 NativeStackTrace::stack = raw_stack;
@@ -238,26 +236,29 @@ bool NativeStackTrace::error_occured() const {
238236
239237bool NativeStackTrace::is_cached (const uint32_t &key) {
240238 try {
241- cache.at (key);
242- return true ;
239+ cache.at (key);
240+ return true ;
243241 }
244242 catch (const std::out_of_range&) {
245- logInfo (3 , " is_cached: no entry for pid %d\n " , key);
243+ logInfo (3 , " is_cached: no entry for pid %d\n " , key);
246244 }
247245 return false ;
248246}
249247
250248UnwindCacheEntry NativeStackTrace::cache_get (const uint32_t &key) {
251- const UnwindCacheEntry & entry = cache.at (key);
249+ const UnwindCacheEntry &entry = cache.at (key);
252250 return entry;
253251}
254252
255253// cache_put adds a new entry to the unwind cache if its capacity allows
256- void NativeStackTrace::cache_put (const uint32_t &key, const unw_cursor_t cursor, const unw_addr_space_t as, void *upt) {
254+ void NativeStackTrace::cache_put (const uint32_t &key, const unw_cursor_t cursor,
255+ const unw_addr_space_t as, void *upt) {
257256 // Check available capacity
258- if (cache_size () > NativeStackTrace::CacheMaxSizeMB*1024 *1024 - cache_single_entry_size ()) {
259- logInfo (2 , " Skipping caching entry for pid %d due to the current cache usage equals to %.2f MB is close to the limit (%d MB)\n " ,
260- key, cache_size_KB ()/1024 , NativeStackTrace::CacheMaxSizeMB);
257+ if (cache_size () > NativeStackTrace::CacheMaxSizeMB*1024 *1024 - cache_single_entry_size ()) {
258+ logInfo (2 ,
259+ " Skipping caching entry for pid %d due to the current cache usage "
260+ " equals to %.2f MB is close to the limit (%d MB)\n " ,
261+ key, cache_size_KB () / 1024 , NativeStackTrace::CacheMaxSizeMB);
261262 return ;
262263 }
263264
@@ -285,27 +286,25 @@ bool NativeStackTrace::cache_delete_key(const uint32_t &key) {
285286}
286287
287288// cache_single_entry_size returns the number of bytes taken by single entry
288- uint32_t NativeStackTrace::cache_single_entry_size () {
289+ uint32_t NativeStackTrace::cache_single_entry_size () {
289290 return sizeof (decltype (cache)::key_type) + sizeof (decltype (cache)::mapped_type);
290291}
291292
292293// cache_size returns the number of bytes currently in use by the cache
293- uint32_t NativeStackTrace::cache_size () {
294+ uint32_t NativeStackTrace::cache_size () {
294295 return sizeof (cache) + cache.size ()*cache_single_entry_size ();
295296}
296297
297298// cache_size_KB returns the number of kilobytes currently in use by the cache
298- float NativeStackTrace::cache_size_KB () {
299- return cache_size ()/1024 ;
300- }
299+ float NativeStackTrace::cache_size_KB () { return cache_size () / 1024 ; }
301300
302301// cache_eviction removes elements older than 5 minutes (CacheMaxTTL=300)
303302void NativeStackTrace::cache_eviction () {
304303 std::vector<uint32_t > keys_to_delete;
305304 float _prev_cache_size = cache_size_KB ();
306305
307- for (std::map<uint32_t , UnwindCacheEntry>::iterator iter = cache.begin (); iter != cache. end (); ++iter)
308- {
306+ for (std::map<uint32_t , UnwindCacheEntry>::iterator iter = cache.begin ();
307+ iter != cache. end (); ++iter) {
309308 uint32_t k = iter->first ;
310309 const UnwindCacheEntry & e = iter->second ;
311310 if (std::abs (difftime (e.timestamp , NativeStackTrace::now)) > NativeStackTrace::CacheMaxTTL) {
@@ -316,7 +315,7 @@ void NativeStackTrace::cache_eviction() {
316315
317316 // Delete expired entries
318317 for ( size_t i = 0 ; i < keys_to_delete.size (); i++ ) {
319- cache_delete_key (keys_to_delete[i]);
318+ cache_delete_key (keys_to_delete[i]);
320319 }
321320
322321 if (keys_to_delete.size () > 0 ) {
0 commit comments