Initial commit
[com/gs-lite.git] / src / tools / qnode.h
1 /* ------------------------------------------------
2 Copyright 2014 AT&T Intellectual Property
3    Licensed under the Apache License, Version 2.0 (the "License");
4    you may not use this file except in compliance with the License.
5    You may obtain a copy of the License at
6
7      http://www.apache.org/licenses/LICENSE-2.0
8
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License.
14  ------------------------------------------- */
15
16 #ifndef __QNODE_H_DEFINED_
17 #define __QNODE_H_DEFINED_
18
19 #include<string>
20
21 struct perf_struct{
22         int n_samples;
23         int init_discard;
24         int init_time;
25         int last_update;
26         unsigned long long int init_stime;
27         unsigned long long int init_utime;
28         unsigned long long int total_stime;
29         unsigned long long int total_utime;
30         double max_stime;
31         double max_utime;
32         double max_time;
33         unsigned long long int max_vm_size;
34         unsigned long long int max_rss;
35
36         void init(){
37                 n_samples = 0;
38                 init_time = 0;
39                 init_stime = 0;
40                 init_utime = 0;
41                 total_stime = 0;
42                 total_utime = 0;
43                 max_stime = 0.0;
44                 max_utime = 0.0;
45                 max_time = 0.0;
46                 max_vm_size = 0;
47                 max_rss = 0;
48                 last_update = -1;
49         }
50
51         perf_struct(){
52                 init();
53         }
54         perf_struct(int d){
55                 init();
56                 init_discard = d;
57         }
58
59         bool update(int t, unsigned long long int u, unsigned long long int s,
60           unsigned long long int v, unsigned long long int r){
61                 if(init_discard>0){
62                         init_discard--;
63                         return false;;
64                 }
65
66
67                 if(v>max_vm_size)
68                         max_vm_size = v;
69                 if(r>max_rss)
70                         max_rss = r;
71                 if(last_update<0){
72                         init_time = t;
73                         init_stime = s;
74                         init_utime = u;
75                 }else{
76                         if(t<=last_update || u<total_utime || s<total_stime){
77                                 return true;
78                         }
79
80                         double tmps,tmpu,tmpt;
81                         tmps = (double)((s-total_stime))/(t-last_update);
82                         tmpu = (double)((u-total_utime))/(t-last_update);
83                         tmpt = tmps+tmpu;
84                         if(tmps>max_stime)
85                                 max_stime = tmps;
86                         if(tmpu>max_utime)
87                                 max_utime = tmpu;
88                         if(tmpt>max_time)
89                                 max_time = tmpt;
90                 }
91                 total_stime = s;
92                 total_utime = u;
93                 last_update = t;
94
95                 return false;
96         }
97
98         std::string to_string(){
99                 char ret[1000];
100                 if(last_update - init_time>0){
101                         sprintf(ret,"stime=%lf, utime=%lf, time=%lf max_stime=%lf, max_utime=%lf, max_time=%lf, max_vmsize=%lld, max_rss = %lld",
102                           (double)(total_stime-init_stime)/(last_update - init_time),
103                           (double)(total_utime-init_utime)/(last_update - init_time),
104                           (double)(total_stime-init_stime)/(last_update - init_time) + (double)(total_utime-init_utime)/(last_update - init_time),
105                                 max_stime,max_utime,max_time,
106                                 max_vm_size,max_rss
107                         );
108                         return ret;
109                 }
110                 return("(no perf results)");
111         }
112
113         static std::string to_csv_hdr(){
114                 return "avg_system_time,avg_user_time,avg_cpu_time,max_sys_time,max_user_time,max_total_time,max_vm_size,max_rss";
115         }
116
117         std::string to_csv(){
118                 char ret[1000];
119                 if(last_update - init_time>0){
120                         sprintf(ret,"%lf,%lf,%lf,%lf,%lf,%lf,%lld,%lld",
121                           (double)(total_stime-init_stime)/(last_update - init_time),
122                           (double)(total_utime-init_utime)/(last_update - init_time),
123                           (double)(total_stime-init_stime)/(last_update - init_time) + (double)(total_utime-init_utime)/(last_update - init_time),
124                                 max_stime,max_utime,max_time,
125                                 max_vm_size,max_rss
126                         );
127                         return ret;
128                 }
129                 return(",,,,,,");
130         }
131
132         double avg_cpu_time(){
133                 return ((double)((total_stime-init_stime)+(total_utime-init_utime))/(last_update - init_time))/100.0;
134         }
135
136         bool is_valid(){
137                 return last_update > 0;
138         }
139 };
140
141
142
143
144 struct field_str{
145         std::string name;
146         int pos;
147         std::string type;
148         std::string mods;
149
150         field_str(){};
151         field_str(std::string n, int p, std::string t, std::string m){
152                 name=n;
153                 pos=p;
154                 type=t;
155                 mods=m;
156         }
157 };
158
159
160 struct qnode{
161         std::string name;
162         std::string executable_name;
163         std::string qnode_type;
164         int aggr_tbl_size;
165         std::vector<field_str *> fields;
166         std::vector<std::string> reads_from;
167         std::vector<int> reads_from_idx;
168         std::vector<int> sources_to_idx;
169         std::string src_interface;
170         int par_index;
171         int start_tick, end_tick;
172
173         unsigned long long int in_tup,out_tup,out_sz;
174         unsigned long long int accepted_tup,cycles,collisions,evictions;
175         double inferred_in_sz;
176         perf_struct *perf;
177
178         qnode(int d){
179                 aggr_tbl_size = 0;
180                 in_tup = 0;
181                 out_tup = 0;
182                 accepted_tup = 0;
183                 cycles = 0;
184                 collisions = 0;
185                 evictions = 0;
186                 inferred_in_sz = 0.0;
187                 par_index = 0;
188                 start_tick = end_tick = -1;
189                 perf = new perf_struct(d);
190         };
191
192         qnode(std::string type, std::string n,int d){
193                 qnode_type=type;
194                 name=n;
195                 aggr_tbl_size = 0;
196
197                 in_tup = 0;
198                 out_tup = 0;
199                 out_sz = 0;
200                 accepted_tup = 0;
201                 cycles = 0;
202                 collisions = 0;
203                 evictions = 0;
204                 inferred_in_sz = 0.0;
205                 par_index = 0;
206                 start_tick = end_tick = -1;
207                 perf = new perf_struct(d);
208         }
209
210         double output_rate(){
211                 if(end_tick == start_tick){
212                         return 0.0;
213                 }
214                 return( ((double)out_sz)/(end_tick-start_tick) );
215         }
216
217         void add_field(field_str *fld){
218                 fields.push_back(fld);
219         }
220
221         void add_source(std::string src){
222                 reads_from.push_back(src);
223         }
224
225 };
226
227
228
229
230
231 #endif