File indexing completed on 2026-04-09 07:49:45
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "sstamp.h"
0023 #include "sproc.h"
0024
0025 struct sprof
0026 {
0027 int64_t st ;
0028 int32_t vm ;
0029 int32_t rs ;
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);
0034 static std::string Desc_(const sprof& prof);
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) ;
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
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
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