Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:45

0001 #pragma once
0002 /**
0003 sprof.h
0004 =========
0005 
0006 Assuming sprof are persisted into an array rp of shape (n,3) with a two name layout::
0007 
0008     In [10]: fold.runprof_names[:4]
0009     Out[10]: array(['SEvt__setIndex_A000', 'SEvt__endIndex_A000', 'SEvt__setIndex_A001', 'SEvt__endIndex_A001'], dtype='<U19')
0010 
0011 Then obtain times and VM, RS with::
0012 
0013     tp = (rp[:,0] - rp[0,0])/1e6         # seconds from first profile stamp
0014     vm = rp[:,1]/1e6                     # GB
0015     rs = rp[:,2]/1e6                     # GB
0016     drm = (rp[1::2,2] - rp[0::2,2])/1e3  # MB between each pair
0017 
0018 See for example ~/opticks/sysrap/tests/sleak.py
0019 
0020 **/
0021 
0022 #include "sstamp.h"
0023 #include "sproc.h"
0024 
0025 struct sprof
0026 {
0027     int64_t st ;  // us microsecond (10^-6) timestamp
0028     int32_t vm ;  // KB
0029     int32_t rs ;  // KB
0030 
0031     static bool Equal(const sprof& a, const sprof& b);
0032     static void Stamp(sprof& prof);
0033     static std::string Serialize(const sprof& prof);  // formerly Desc
0034     static std::string Desc_(const sprof& prof);  // dont call Desc for now as need to change all Desc to Serialize
0035     static int         Import(sprof& prof, const char* str);
0036 
0037     static sprof Diff(const sprof& p0, const sprof& p1);
0038     static int64_t Delta_ST(const sprof* p0, const sprof* p1) ;
0039     static int32_t Delta_VM(const sprof* p0, const sprof* p1) ;
0040     static int32_t Delta_RS(const sprof* p0, const sprof* p1) ;
0041 
0042     static std::string Desc(const sprof& p0, const sprof& p1);
0043     static std::string Now();
0044     static bool LooksLikeProfileTriplet(const char* str);
0045 
0046 };
0047 
0048 inline bool sprof::Equal(const sprof& a, const sprof& b)
0049 {
0050     return a.st == b.st && a.vm == b.vm && a.rs == b.rs ;
0051 }
0052 
0053 inline void sprof::Stamp(sprof& p)
0054 {
0055     p.st = sstamp::Now();
0056     sproc::Query(p.vm, p.rs) ;  // sprof::Stamp
0057 }
0058 
0059 inline std::string sprof::Serialize(const sprof& prof)
0060 {
0061     char delim = ',' ;
0062     std::stringstream ss ;
0063     ss << prof.st << delim << prof.vm << delim << prof.rs ;
0064     std::string str = ss.str();
0065     return str ;
0066 }
0067 
0068 inline std::string sprof::Desc_(const sprof& prof)
0069 {
0070     std::stringstream ss ;
0071     ss << std::setw(20) << prof.st
0072        << std::setw(10) << prof.vm
0073        << std::setw(10) << prof.rs
0074        << " " << sstamp::Format(prof.st)
0075        ;
0076     std::string str = ss.str();
0077     return str ;
0078 }
0079 
0080 inline int sprof::Import(sprof& prof, const char* str)
0081 {
0082     if(!LooksLikeProfileTriplet(str)) return 1 ;
0083     char* end ;
0084     prof.st = strtoll( str, &end, 10 ) ;
0085     prof.vm = strtoll( end+1, &end, 10 ) ;
0086     prof.rs = strtoll( end+1, &end, 10 ) ;
0087     return 0 ;
0088 }
0089 
0090 inline sprof sprof::Diff(const sprof& p0, const sprof& p1)
0091 {
0092     sprof df ;
0093     df.st = p1.st - p0.st ;
0094     df.vm = p1.vm - p0.vm ;
0095     df.rs = p1.rs - p0.rs ;
0096     return df ;
0097 }
0098 
0099 inline int64_t sprof::Delta_ST(const sprof* p0, const sprof* p1)
0100 {
0101     return p0 && p1 ? p1->st - p0->st : -1 ;
0102 }
0103 inline int32_t sprof::Delta_VM(const sprof* p0, const sprof* p1)
0104 {
0105     return p0 && p1 ? p1->vm - p0->vm : -1 ;
0106 }
0107 inline int32_t sprof::Delta_RS(const sprof* p0, const sprof* p1)
0108 {
0109     return p0 && p1 ? p1->rs - p0->rs : -1 ;
0110 }
0111 
0112 
0113 inline std::string sprof::Desc(const sprof& p0, const sprof& p1)
0114 {
0115     sprof df = Diff(p0, p1) ;
0116     std::stringstream ss ;
0117     ss << Desc_(p0) << std::endl ;
0118     ss << Desc_(p1) << std::endl ;
0119     ss << Desc_(df) << std::endl ;
0120     std::string str = ss.str();
0121     return str ;
0122 }
0123 
0124 
0125 inline std::string sprof::Now()
0126 {
0127     sprof now ;
0128     Stamp(now);
0129     return Serialize(now);
0130 }
0131 
0132 
0133 
0134 /**
0135 sprof::LooksLikeProfileTriplet
0136 --------------------------------
0137 
0138 sprof strings have:
0139 
0140 1. first field of 16 digits, see sstamp::LooksLikeStampInt to understand why 16
0141 2. two ',' delimiters
0142 3. only digits and delimiters
0143 
0144 For example::
0145 
0146     "1700367820015746,4370198,884"
0147 
0148 Also allow spaces on the end::
0149 
0150     "1700367820015746,4370198,884 "
0151     "1700367820015746,4370198,884  "
0152     "1700367820015746,4370198,884   "
0153 
0154 **/
0155 
0156 inline bool sprof::LooksLikeProfileTriplet(const char* str)
0157 {
0158     int len = str ? int(strlen(str)) : 0 ;
0159     int count_delim = 0 ;
0160     int count_non_digit = 0 ;
0161     int first_field_digits = 0 ;
0162 
0163     for(int i=0 ; i < len ; i++ )
0164     {
0165         char c = str[i] ;
0166         bool is_space = c == ' ' ;
0167         bool is_digit = c >= '0' && c <= '9' ;
0168         bool is_delim = c == ',' ;
0169         if(!is_digit && !is_space) count_non_digit += 1 ;
0170         if(count_delim == 0 && is_digit ) first_field_digits += 1 ;
0171         if(is_delim) count_delim += 1 ;
0172     }
0173     bool heuristic = count_delim == 2 && count_non_digit == count_delim && first_field_digits == 16 ;
0174     return heuristic ;
0175 }
0176