modified command line args to psuedoparadyn
[dyninst.git] / visi / test / client / src / psuedoparadyn.C
1 /* $Log: psuedoparadyn.C,v $
2 /* Revision 1.3  1995/10/14 21:01:45  newhall
3 /* modified command line args to psuedoparadyn
4 /*
5  * Revision 1.2  1995/09/27  16:30:15  newhall
6  * added option for sending user specified data values to a visi
7  *
8  * Revision 1.1  1995/09/18  18:26:48  newhall
9  * updated test subdirectory, added visilib routine GetMetRes()
10  * */ 
11 #include <stdio.h>
12 #include <stream.h>
13 #include "util/h/String.h"
14 #include "util/h/Vector.h"
15 #include "util/h/makenan.h"
16 #include "visi.xdr.CLNT.h" 
17
18
19 #define DEFAULT_BUCKET_WIDTH 0.2    // initial width of each histogram bucket
20 #define DEFAULT_NUM_BUCKETS  1000   // number of histogram buckets
21
22 // global variables
23 visualizationUser *visip;
24 int currPhaseId = 0;
25 int lastBucketFilled = 0;
26 double bucketWidth = DEFAULT_BUCKET_WIDTH;
27 double minbucketWidth = DEFAULT_BUCKET_WIDTH;
28 int numBuckets = DEFAULT_NUM_BUCKETS;
29 u_int nextMetId = 0;
30 u_int nextResId = 0;
31 vector<T_visi::visi_matrix> mrlist;
32 void SendData(u_int,u_int,int &,double &);
33 void Fold(double &bucketWidth,int &lastBucketCollected);
34 void NewMR();
35 void NewPhase(int &currId,double binSize,int lastBucket,double minWidth);
36
37 // upcall routines for visualization
38 void visualizationUser::GetMetricResource(string metric_res,int num, int type){
39
40   cerr << "## in visualizationUser::GetMetricResource " << endl;
41   NewMR();
42 }
43
44
45 void visualizationUser::StopMetricResource(u_int metricId,u_int resourceId){
46
47   cerr << "## in visualizationUser::StopMetricResource " << endl;
48   cerr << "## metId = " << metricId << " resId = " << resourceId << endl;
49   u_int size = mrlist.size();
50   for(unsigned i=0; i < size; i++){
51       if((metricId == mrlist[i].met.Id) && (resourceId == mrlist[i].res.Id)){
52           mrlist[i] = mrlist[(size - 1)];
53           mrlist.resize(size - 1);
54           return;
55   }}
56 }
57
58 void visualizationUser::StartPhase(double begin,string name){
59
60   cerr << "## in visualizationUser::StartPhase " << endl;
61   cerr << "## begin = " << begin << " name = " << name.string_of() << endl;
62   NewPhase(currPhaseId,bucketWidth,lastBucketFilled,minbucketWidth);
63 }
64
65 void visualizationUser::GetPhaseInfo(){
66     cerr << "## in visualizationUser::GetPhaseInfo upcall" << endl;
67     cerr << "##     this routine is not supported" << endl;
68 }
69
70
71 main(int argc, char *argv[]){
72
73
74     if(argc < 2){
75         cerr << "Error: command must be in following form:" << endl; 
76         cerr << " (1)  psuedoparadyn server_executable <server_args>" << endl;
77         exit(-1);
78     }
79
80     int pid;
81     vector<string> arg_list;
82     
83     if(argc > 2){
84         unsigned index = 2;
85         while(argv[index]){
86             arg_list += argv[index];
87             index++;
88         } 
89     }
90
91     int fd = RPCprocessCreate(pid, "localhost", "", argv[1], arg_list);
92     if(fd < 0) exit(-1);
93     visip = new visualizationUser(fd,NULL,NULL,false);
94    
95     // send initial set of metrics and foci to visi
96
97
98     // enter loop
99     bool done = false;
100     while(!done){
101
102         while (visip->buffered_requests()) {
103               if (visip->process_buffered() == T_visi::error) {
104                   cerr << "error on visi\n";
105                   assert(0);
106             }
107         }
108         if(RPC_readReady(fd)){
109             if(visip->waitLoop() == T_visi::error){
110                 cerr << "error on visi\n";
111                 assert(0);
112         }}
113         else {
114             int wch = -1;
115             while((wch < 0) || (wch > 9)){
116                 cerr << endl << "----------------------------------" << endl;
117                 cerr << "Enter number of operation to perform:" << endl;
118                 cerr << "    0: add random data values (values are between 0 and 100)" << endl;
119                 cerr << "    1: add NULL data values" << endl;
120                 cerr << "    2: send ZERO data values   " << endl;
121                 cerr << "    3: send user specified data Values"  << endl;    
122                 cerr << "    4: fold" << endl;
123                 cerr << "    5: start a new phase" << endl;
124                 cerr << "    6: add new Met/Res" << endl;
125                 cerr << "    7: print all active metrics and resources" << endl;
126                 cerr << "    8: continue (do nothing--useful for receiving upcalls from visi process)"   << endl;
127                 cerr << "    9: quit" << endl;
128                 cerr << endl << "----------------------------------" << endl;
129                 scanf("%d",&wch);
130             }
131             switch(wch){
132                 case 0:
133                     cerr << "enter number of buckets to send" << endl;
134                     scanf("%d",&wch);
135                     SendData(wch,0,lastBucketFilled,bucketWidth);
136                     break;
137                 case 1:
138                     cerr << "enter number of NULL buckets to send" << endl;
139                     scanf("%d",&wch);
140                     SendData(wch,1,lastBucketFilled,bucketWidth);
141                     break;
142                 case 2:
143                     cerr << "enter number of ZERO buckets to send" << endl;
144                     scanf("%d",&wch);
145                     SendData(wch,2,lastBucketFilled,bucketWidth);
146                     break;
147                 case 3:
148                     cerr << "enter number of buckets to send" << endl;
149                     scanf("%d",&wch);
150                     SendData(wch,3,lastBucketFilled,bucketWidth);
151                     break;
152                 case 4:
153                     Fold(bucketWidth,lastBucketFilled);
154                     break;
155                 case 5:
156                     NewPhase(currPhaseId,bucketWidth,
157                              lastBucketFilled,minbucketWidth);
158                     break;
159                 case 6:
160                     NewMR();
161                     break;
162                 case 7:
163                     {
164                         cerr << "LIST OF ENABLED METRIC/FOCUS PAIRS" << endl;
165                         for(unsigned i=0; i < mrlist.size(); i++){
166                             cerr << "    metric[" << i << "] = " << 
167                                   mrlist[i].met.name.string_of() << " res[" <<
168                                   i << "] = " << mrlist[i].res.name.string_of()
169                                   << endl;
170                         }
171                     }
172                     break;
173                 case 8:
174                     break;
175                 case 9:
176                     done = true;
177                     break;
178                 default:
179                     break;
180             }
181         }
182     }
183
184 }
185
186 void SendData(u_int howmany, 
187               u_int type_values,
188               int &lastBucketCollected,
189               double &bucketWidth){
190
191     if(howmany == 0) return;
192
193     float user_selected_value = 0.0;
194     if(type_values == 3){
195         cerr << "enter data value as a float" << endl;
196         scanf("%f",&user_selected_value);
197     }
198     vector <T_visi::dataValue> tempData;
199     for(unsigned i=0; i < howmany; i++){
200         for(unsigned j=0; j < mrlist.size(); j++){
201             T_visi::dataValue newValue;
202             newValue.metricId = mrlist[j].met.Id;
203             newValue.resourceId = mrlist[j].res.Id;
204             newValue.bucketNum = lastBucketCollected;
205             newValue.data = 0.0;
206             if(type_values == 1){
207                 newValue.data = PARADYN_NaN; 
208             }
209             else if(type_values == 0){
210                 newValue.data = 0.1*(rand() % 1000);
211             }
212             else if(type_values == 3){
213                 newValue.data = user_selected_value;
214             }
215             tempData += newValue;
216         }
217         lastBucketCollected++;
218         if(lastBucketCollected > numBuckets){
219             // send new data values, then do a fold
220             visip->Data(tempData);
221             tempData.resize(0);
222             Fold(bucketWidth,lastBucketCollected);
223         }
224     }
225     visip->Data(tempData);
226 }
227
228 void Fold(double &bucketWidth,int &lastBucketCollected){
229     bucketWidth *=2;
230     lastBucketCollected /=2;
231     visip->Fold(bucketWidth);
232 }
233
234 void NewPhase(int &currId,double binSize,int lastBucket,double minWidth){
235
236     double currTime = binSize*lastBucket;
237     if(currId > 0)
238         visip->PhaseEnd(currTime,currId);
239     currId++;
240     char temp[32];
241     sprintf(temp,"%s%d","phase_",currId);
242     string name = temp;
243     visip->PhaseStart(currTime,-1.0,minWidth,name,currId);
244 }
245
246 void NewMR(){
247
248     cerr << "Add new metrics and resources to visualization:" << endl;
249     cerr << "Enter number of metrics and number of resources" << endl;
250     unsigned num_mets = 0, num_res = 0;
251     scanf("%d%d",&num_mets,&num_res);
252     vector<T_visi::visi_matrix> newlist;
253     for(unsigned i=0; i < num_mets; i++){
254         for(unsigned j=0; j < num_res; j++){
255             T_visi::visi_matrix newElement;
256             char temp[32];
257             sprintf(temp, "%s%d","metric_",nextMetId);
258             newElement.met.name = temp;
259             sprintf(temp, "%s%d","units_",nextMetId);
260             newElement.met.units = temp;
261             newElement.met.Id = nextMetId;
262             newElement.met.aggregate = 1;
263             sprintf(temp, "%s%d","resource_",nextResId+j);
264             newElement.res.name = temp;
265             newElement.res.Id = nextResId+j;
266             newlist += newElement;
267         }
268         nextMetId++;
269     }
270     nextResId += num_res;
271     visip->AddMetricsResources(newlist,bucketWidth,numBuckets,0.0,-1);
272     mrlist += newlist;
273 }
274