-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOGLES2PVRScopeExampleS4.cpp
More file actions
726 lines (585 loc) · 18 KB
/
Copy pathOGLES2PVRScopeExampleS4.cpp
File metadata and controls
726 lines (585 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include <GLES2/gl2.h>
#include "PVRScopeGraph.h"
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
//
#include <android/log.h>
#include <android/bitmap.h>
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , "PVRScope", __VA_ARGS__)
#include "PVRScopeStats.h"
// Step 1. Define a function to initialise PVRScopeStats
bool PSInit(SPVRScopeImplData **ppsPVRScopeData, SPVRScopeCounterDef **ppsCounters, SPVRScopeCounterReading* const psReading, unsigned int* const pnCount)
{
//Initialise PVRScope
const EPVRScopeInitCode eInitCode = PVRScopeInitialise(ppsPVRScopeData);
if(ePVRScopeInitCodeOk == eInitCode)
{
printf("Initialised services connection.\n");
}
else
{
printf("Error: failed to initialise services connection.\n");
*ppsPVRScopeData = NULL;
return false;
}
//Initialse the counter data structures.
if (PVRScopeGetCounters(*ppsPVRScopeData, pnCount, ppsCounters, psReading))
{
printf("Total counters enabled: %d.", *pnCount);
}
return true;
}
double prev_total[4] = {0,0,0,0};
double prev_idle[4] = {0,0,0,0};
double csit[4][3] = {{0,0,0},{0,0,0},{0,0,0}, {0,0,0}};
double csiu[4][3] = {{0,0,0},{0,0,0},{0,0,0}, {0,0,0}};
double prev_tx = 0;
double prev_rx = 0;
double parseMem(char totalBuf[], char freeBuf[], char bufferBuf[])
{
double ret = 0;
double total = 0;
double free = 0;
double buffer = 0;
char *tok = NULL;
tok = strtok(totalBuf, " ");
tok = strtok(NULL, " ");
total = atof(tok);
//printf("%s\n", tok);
tok = strtok(freeBuf, " ");
tok = strtok(NULL, " ");
free = atof(tok);
//printf("%f\n", free);
tok = strtok(bufferBuf, " ");
tok = strtok(NULL, " ");
buffer = atof(tok);
//printf("%f\n", buffer);
ret = (total - (free+buffer))/total;
ret = ret * 100;
return ret;
}
double parseCPU(char cpuLine[],int core)
{
//printf("prev total = %f\n",prev_total);
double total = 0;
double idle = 0;
char *tok = NULL;
tok = strtok(cpuLine, " ");
int count = 0;
while(tok)
{
//printf("Token: %s\n", tok);
if(count > 0){
total += atof(tok);
}
if(count == 4){
idle = atof(tok);
}
tok = strtok(NULL, " ");
++count;
}
double diff_idle = idle - prev_idle[core];
double diff_total = total - prev_total[core];
double diff_util = (1000 * (diff_total - diff_idle) / diff_total) / 10;
prev_total[core] = total;
prev_idle[core] = idle;
free(tok);
//printf("parse CPU util = %f\n",diff_util);
return diff_util;
}
void *callEvent(void *ptr){
/*char aut2[1024];
sprintf(aut2,"sh /data/local/tmp/gpu_train_event.sh");
system(aut2);*/
}
char buffer[4096];
char header[9999];
char saveFile[2048];
int numCol = 9999;
char **sample_cpu;
FILE *fp_cpu;
FILE *fp_cpu_chk;
FILE *fp;
FILE *fp_save;
FILE *fp_mem;
int nTest = 0; // = atoi(argv[1]);
int nRows = 0; //atoi(argv[2]);
int delay = 0;
int start_strc = 0;
int stop_strc = 0;
int start_load = 50000;
int stop_load = 45000;
void method()
{
sample_cpu = (char **)malloc(nRows * sizeof(char *));
int c2=0;
for(c2=0; c2<nRows; c2++)
sample_cpu[c2] = (char *) malloc(numCol * sizeof(char));
printf("test passed2\n");
SPVRScopeImplData *psData;
//Counter information (set at init time)
SPVRScopeCounterDef *psCounters;
unsigned int uCounterNum;
//Counter reading data
unsigned int uActiveGroup;
unsigned int uActiveGroupSelect;
bool bActiveGroupChanged;
SPVRScopeCounterReading sReading;
// Step 2. Initialise PVRScopeStat
if (PSInit(&psData, &psCounters, &sReading, &uCounterNum)){
//LOGI("PVRScope up and running.");
printf("PVRScore up and running...\n");
/*sprintf(aut,"%s\n","su");
system(aut);*/
}else{
//LOGE("Error initializing PVRScope.");
printf("Error initializing PVRScope...\n");
}
bActiveGroupChanged = true;
uActiveGroupSelect = 0;
unsigned int sampleRate = delay;
unsigned int index = 0;
unsigned int j = 0;
unsigned int k = 0;
printf("Begin working...\n");
strcat(header,"delay=");
char delayData[50];
snprintf(delayData,50,"%d\n",delay);
strcat(header,delayData);
while (j < nRows)
{
// Ask for the active group 0 only on the first run. Then set it to 0xffffffff
if(bActiveGroupChanged)
{
uActiveGroup = uActiveGroupSelect;
}
else
{
uActiveGroup = 0xffffffff;
}
++index;
if (index < 100)
{
// Sample the counters every 10ms. Don't read or output it.
PVRScopeReadCountersThenSetGroup(psData, NULL, 0, uActiveGroup);
}
else
{
if(k < delay)
{
++k;
index = 0;
continue;
}
k = 0;
index = 0;
printf("sample %d\n",j);
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long time_in_mill = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
// Step 4. Read and output the counter information for group 0 to Logcat
if(PVRScopeReadCountersThenSetGroup(psData, &sReading, time_in_mill * 1000, uActiveGroup))
{
printf("test test\n");
//if(j == (4*60) / delay)
if(j==start_strc)
{
char command[1024];
sprintf(command,"./data/local/tmp/strc %d %d &",start_load,stop_load);
system(command);
}
//if(j == (15*60) / delay)
if(j==stop_strc)
{
char command2[1024];
sprintf(command2,"./data/local/tmp/busybox killall strc");
system(command2);
}
strcat(header,"\n_LOOP_");
char loop[50];
snprintf(loop,50,"%d\n",j);
strcat(header,loop);
//Read bigLittle_status
if((fp_cpu = fopen("/dev/bL_status","r")) != NULL)
{
//printf("bl\n");
strcat(header,"_BL_\n");
fgets(buffer,sizeof buffer, fp_cpu);
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,buffer);
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp_cpu);
}
strcat(header,"_CPU_\n");
if((fp_cpu = fopen("/proc/stat","r")) != NULL)
{
fgets(buffer,sizeof buffer, fp_cpu);
//CPU0
fgets(buffer,sizeof buffer, fp_cpu);
double cpu_util = parseCPU(buffer,0);
char output[50];
snprintf(output,50,"util=%.2f",cpu_util);
strcat(header,output);
memset(buffer, 0, sizeof(buffer));
memset(output, 0, sizeof(output));
if((fp_cpu_chk = fopen("/sys/devices/system/cpu/cpu1/online","r")) != NULL){
char test[50];
fgets(test, sizeof test, fp_cpu_chk);
if(atoi(test) == 1){
fgets(buffer,sizeof buffer, fp_cpu);
cpu_util = parseCPU(buffer,1);
snprintf(output,50," %.2f",cpu_util);
strcat(header,output);
memset(buffer, 0, sizeof(buffer));
memset(output, 0, sizeof(output));
//fclose(fp_cpu);
}
else
{
strcat(header," x");
}
fclose(fp_cpu_chk);
}
if((fp_cpu_chk = fopen("/sys/devices/system/cpu/cpu2/online","r")) != NULL){
char test[50];
fgets(test, sizeof test, fp_cpu_chk);
if(atoi(test) == 1){
fgets(buffer,sizeof buffer, fp_cpu);
cpu_util = parseCPU(buffer,2);
snprintf(output,50," %.2f",cpu_util);
strcat(header,output);
memset(buffer, 0, sizeof(buffer));
memset(output, 0, sizeof(output));
//fclose(fp_cpu);
}
else
{
strcat(header," x");
//printf(" x");
}
fclose(fp_cpu_chk);
}
if((fp_cpu_chk = fopen("/sys/devices/system/cpu/cpu3/online","r")) != NULL){
char test[50];
fgets(test, sizeof test, fp_cpu_chk);
if(atoi(test) == 1)
{
fgets(buffer,sizeof buffer, fp_cpu);
cpu_util = parseCPU(buffer,3);
snprintf(output,50," %.2f",cpu_util);
strcat(header,output);
memset(buffer, 0, sizeof(buffer));
memset(output, 0, sizeof(output));
//fclose(fp_cpu);
}
else
{
strcat(header," x");
//printf("last x\n");
}
fclose(fp_cpu_chk);
}
fclose(fp_cpu);
}
printf(">> %s\n <<",header);
strcat(header,"\n_FREQ_");
//Read freq0
if((fp_cpu = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq","r")) != NULL)
{
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,"\nfreq0=");
strcat(header,buffer);
printf("%s\n",header);
memset(buffer, 0, sizeof(buffer));
}
fclose(fp_cpu);
//Read freq1
if((fp_cpu = fopen("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq","r")) != NULL)
{
printf("freq1\n");
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,"freq1=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp_cpu);
}
else
{
strcat(header,"freq1=x");
printf("freq1=x");
}
//Read freq2
if((fp_cpu = fopen("/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq","r")) != NULL) {
printf("freq2\n");
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,"freq2=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp_cpu);
}
else
{
strcat(header,"\nfreq2=x");
printf("\nfreq2=x");
}
//Read freq3
if((fp_cpu = fopen("/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq","r")) != NULL) {
printf("freq3\n");
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header,"freq3=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp_cpu);
}
else
{
strcat(header,"\nfreq3=x");
printf("\nfreq3=x");
}
printf("%s\n",header);
//Read CPU idle time C0S0IT //////////////////////////////////////////////////////////////////////////
strcat(header,"\n_IDLE_TIME_\n");
printf("idle time\n");
for(int core=0; core<4; core++)
{
char title[100];
snprintf(title,100,"idle_time_%d=",core);
strcat(header,title);
for(int state=0; state<3; state++)
{
char proc[100];
sprintf(proc,"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/time",core,state);
if((fp_cpu = fopen(proc,"r")) != NULL)
{
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header," ");
double cur = atof(buffer);
double diff_time = cur - csit[core][state];
char output_idle[50];
snprintf(output_idle,50,"%.2f",diff_time/1000);
strcat(header,output_idle);
memset(buffer, 0, sizeof(buffer));
csit[core][state] = cur;
fclose(fp_cpu);
}
else{
strcat(header," x");
}
}
strcat(header,"\n");
}
strcat(header,"_IDLE_USAGE_\n");
for(int core=0; core<4; core++)
{
char title[100];
snprintf(title,100,"idle_usage_%d=",core);
strcat(header,title);
for(int state=0; state<3; state++)
{
char proc[100];
sprintf(proc,"/sys/devices/system/cpu/cpu%d/cpuidle/state%d/usage",core,state);
if((fp_cpu = fopen(proc,"r")) != NULL)
{
fgets(buffer,sizeof buffer, fp_cpu);
strcat(header," ");
int cur = atoi(buffer);
int diff_entry = cur - csiu[core][state];
char output_entry[50];
snprintf(output_entry,50,"%d",diff_entry);
strcat(header,output_entry);
memset(buffer, 0, sizeof(buffer));
csiu[core][state] = cur;
fclose(fp_cpu);
}
else{
strcat(header," x");
}
}
strcat(header,"\n");
}
///////////////////////// Read memory usage /////////////////////////////////////////
strcat(header,"_MEM_\n");
//if((fp = fopen("/data/local/tmp/busybox free -m", "r")) != NULL)
if((fp_mem = fopen("/proc/meminfo","r")) != NULL)
{
//printf("mem\n");
fgets(buffer, sizeof buffer, fp_mem);
//printf("buffer = %s\n",buffer);
strcat(header,"mem=");
char buffer2[1024];
char buffer3[1024];
fgets(buffer2, sizeof buffer, fp_mem);
fgets(buffer3, sizeof buffer, fp_mem);
double mem_util = parseMem(buffer, buffer2, buffer3);
char output[50];
snprintf(output,50,"%.2f",mem_util);
strcat(header,output);
memset(buffer, 0, sizeof(buffer));
memset(buffer2, 0, sizeof(buffer2));
memset(buffer3, 0, sizeof(buffer3));
fclose(fp_mem);
}
strcat(header,"\n_DISPLAY_");
//Read bright
if((fp = fopen("/sys/class/backlight/panel/brightness","r")) != NULL) {
//printf("brightness\n");
fgets(buffer,sizeof buffer, fp);
//sprintf(header,"\n_DISPLAY_\n%s","bright=");
strcat(header,"\nbright=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
strcat(header,"_WIFI_\n");
//wifi
if((fp = fopen("/sys/class/net/wlan0/statistics/tx_packets","r")) != NULL) {
//printf("WiFi\n");
fgets(buffer,sizeof buffer, fp);
double cur = atof(buffer);
double diff_tx = cur - prev_tx;
char output_tx[50];
snprintf(output_tx,50,"%.2f",diff_tx);
strcat(header,"tx=");
strcat(header,output_tx);
memset(buffer, 0, sizeof(buffer));
prev_tx = cur;
fclose(fp);
}
else{
strcat(header,"tx=0");
}
if((fp = fopen("/sys/class/net/wlan0/statistics/rx_packets","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
double cur = atof(buffer);
double diff_rx = cur - prev_rx;
char output_rx[50];
snprintf(output_rx,50,"%.2f",diff_rx);
strcat(header,"\nrx=");
strcat(header,output_rx);
memset(buffer, 0, sizeof(buffer));
prev_rx = cur;
fclose(fp);
}
else{
strcat(header,"\nrx=0");
}
if((fp = fopen("/sys/class/net/wlan0/operstate","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
strcat(header,"\noperstate=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
else{
strcat(header,"\noperstate=0");
}
strcat(header,"\n_BATTERY_\n");
if((fp = fopen("/sys/class/power_supply/battery/voltage_now","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
strcat(header,"batt_volt=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
if((fp = fopen("/sys/class/power_supply/battery/current_now","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
strcat(header,"batt_current=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
if((fp = fopen("/sys/class/power_supply/battery/capacity","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
strcat(header,"batt_capacity=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
if((fp = fopen("/sys/class/power_supply/battery/temp","r")) != NULL) {
fgets(buffer,sizeof buffer, fp);
strcat(header,"batt_capacity=");
strcat(header,buffer);
memset(buffer, 0, sizeof(buffer));
fclose(fp);
}
// Check for all the counters in the system if the counter has a value on the given active group and ouptut it.
strcat(header,"_GPU_\n");
for(int p = 0; p < uCounterNum; ++p)
{
if(p < sReading.nValueCnt)
{
strcat(header,psCounters[p].pszName);
strcat(header,"=");
char params[50];
snprintf(params,50,"%.2f\n",sReading.pfValueBuf[p]);
strcat(header, params);
//memset(buffer, 0, sizeof(buffer));
}
}
}
strcpy(sample_cpu[j],header);
printf("%s\n",sample_cpu[j]);
memset(header, 0, sizeof(header));
printf("End_loop %d\n",j);
++j;
}
//Poll for the counters once a second
usleep(10000);
//usleep(1000000);
}
// Step 5. Shutdown PVRScopeStats
PVRScopeDeInitialise(&psData, &psCounters, &sReading);
printf("save file\n");
sprintf(saveFile,"/data/local/tmp/stat/sample%d.txt", nTest);
fp_save = fopen(saveFile,"w+");
for(int i = 0; i < nRows; i++)
{
printf("%s",sample_cpu[i]);
fprintf(fp_save, "%s", sample_cpu[i]);
}
printf("close all file\n");
fclose(fp_save);
printf("end file\n");
//Clear array memory
//sample is a heap data structure
free(sample_cpu);
memset(saveFile, 0, sizeof(saveFile));
memset(prev_total, 0, sizeof(prev_total));
memset(prev_idle, 0, sizeof(prev_idle));
memset(csit, 0, sizeof(csit));
memset(csiu, 0, sizeof(csiu));
prev_tx = 0;
prev_rx = 0;
printf("clear file\n");
exit(0);
}
int main(int argc, char **argv)
{
printf("Start GPU testing...\n");
//pthread_t thread1;
//const char *message1 = "Thread 1";
//int iret1;
if(argc < 4)
{
printf("Please put ./sample fileIndex NumberOfsample delay(second) sample_of_start_strc sample_of_stop_strc sample_of_start_load sample_of_stop_load\n");
}
else
{
nTest = atoi(argv[1]);
nRows = atoi(argv[2]);
delay = atoi(argv[3]);
start_strc = atoi(argv[4]);
stop_strc = atoi(argv[5]);
start_load = atoi(argv[6]);
stop_load = atoi(argv[7]);
//iret1 = pthread_create(&thread1, NULL, method, (void*) message1);
method();
}
return 0;
}