gator: Version 5.15
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / SessionData.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2013. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <string.h>
10 #include "SessionData.h"
11 #include "SessionXML.h"
12 #include "Logging.h"
13
14 SessionData* gSessionData = NULL;
15
16 SessionData::SessionData() {
17         initialize();
18 }
19
20 SessionData::~SessionData() {
21 }
22
23 void SessionData::initialize() {
24         mWaitingOnCommand = false;
25         mSessionIsActive = false;
26         mLocalCapture = false;
27         mOneShot = false;
28         readCpuInfo();
29         mConfigurationXMLPath = NULL;
30         mSessionXMLPath = NULL;
31         mEventsXMLPath = NULL;
32         mTargetPath = NULL;
33         mAPCDir = NULL;
34         mSampleRate = 0;
35         mLiveRate = 0;
36         mDuration = 0;
37         mBacktraceDepth = 0;
38         mTotalBufferSize = 0;
39         // sysconf(_SC_NPROCESSORS_CONF) is unreliable on 2.6 Android, get the value from the kernel module
40         mCores = 1;
41 }
42
43 void SessionData::parseSessionXML(char* xmlString) {
44         SessionXML session(xmlString);
45         session.parse();
46
47         // Set session data values
48         if (strcmp(session.parameters.sample_rate, "high") == 0) {
49                 mSampleRate = 10000;
50         } else if (strcmp(session.parameters.sample_rate, "normal") == 0) {
51                 mSampleRate = 1000;
52         } else if (strcmp(session.parameters.sample_rate, "low") == 0) {
53                 mSampleRate = 100;
54         } else if (strcmp(session.parameters.sample_rate, "none") == 0) {
55                 mSampleRate = 0;
56         } else {
57                 logg->logError(__FILE__, __LINE__, "Invalid sample rate (%s) in session xml.", session.parameters.sample_rate);
58                 handleException();
59         }
60         mBacktraceDepth = session.parameters.call_stack_unwinding == true ? 128 : 0;
61         mDuration = session.parameters.duration;
62
63         // Determine buffer size (in MB) based on buffer mode
64         mOneShot = true;
65         if (strcmp(session.parameters.buffer_mode, "streaming") == 0) {
66                 mOneShot = false;
67                 mTotalBufferSize = 1;
68         } else if (strcmp(session.parameters.buffer_mode, "small") == 0) {
69                 mTotalBufferSize = 1;
70         } else if (strcmp(session.parameters.buffer_mode, "normal") == 0) {
71                 mTotalBufferSize = 4;
72         } else if (strcmp(session.parameters.buffer_mode, "large") == 0) {
73                 mTotalBufferSize = 16;
74         } else {
75                 logg->logError(__FILE__, __LINE__, "Invalid value for buffer mode in session xml.");
76                 handleException();
77         }
78
79         mImages = session.parameters.images;
80         // Convert milli- to nanoseconds
81         mLiveRate = session.parameters.live_rate * (int64_t)1000000;
82         if (mLiveRate > 0 && mLocalCapture) {
83                 logg->logMessage("Local capture is not compatable with live, disabling live");
84                 mLiveRate = 0;
85         }
86 }
87
88 void SessionData::readCpuInfo() {
89         char temp[256]; // arbitrarily large amount
90         strcpy(mCoreName, "unknown");
91         mCpuId = -1;
92
93         FILE* f = fopen("/proc/cpuinfo", "r");  
94         if (f == NULL) {
95                 logg->logMessage("Error opening /proc/cpuinfo\n"
96                         "The core name in the captured xml file will be 'unknown'.");
97                 return;
98         }
99
100         bool foundCoreName = false;
101         bool foundCpuId = false;
102         while (fgets(temp, sizeof(temp), f) && (!foundCoreName || !foundCpuId)) {
103                 if (strlen(temp) > 0) {
104                         temp[strlen(temp) - 1] = 0;     // Replace the line feed with a null
105                 }
106
107                 const bool foundHardware = strstr(temp, "Hardware") != 0;
108                 const bool foundCPUPart = strstr(temp, "CPU part") != 0;
109                 if (foundHardware || foundCPUPart) {
110                         char* position = strchr(temp, ':');
111                         if (position == NULL || (unsigned int)(position - temp) + 2 >= strlen(temp)) {
112                                 logg->logMessage("Unknown format of /proc/cpuinfo\n"
113                                         "The core name in the captured xml file will be 'unknown'.");
114                                 return;
115                         }
116                         position += 2;
117
118                         if (foundHardware) {
119                                 strncpy(mCoreName, position, sizeof(mCoreName));
120                                 mCoreName[sizeof(mCoreName) - 1] = 0; // strncpy does not guarantee a null-terminated string
121                                 foundCoreName = true;
122                         }
123
124                         if (foundCPUPart) {
125                                 int cpuId = strtol(position, NULL, 16);
126                                 if (cpuId > mCpuId) {
127                                         mCpuId = cpuId;
128                                 }
129                                 foundCpuId = true;
130                         }
131                 }
132         }
133
134         if (!foundCoreName) {
135                 logg->logMessage("Could not determine core name from /proc/cpuinfo\n"
136                                                  "The core name in the captured xml file will be 'unknown'.");
137         }
138         fclose(f);
139  }
140
141 int getEventKey() {
142         // Start one after the gator.ko's value of 1
143         static int key = 2;
144
145         const int ret = key;
146         key += 2;
147         return ret;
148 }