Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 2 | * Copyright (C) ARM Limited 2010-2014. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 9 | #include "SessionData.h" |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 10 | |
| 11 | #include <string.h> |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 12 | #include <sys/mman.h> |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 13 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 14 | #include "SessionXML.h" |
| 15 | #include "Logging.h" |
| 16 | |
| 17 | SessionData* gSessionData = NULL; |
| 18 | |
| 19 | SessionData::SessionData() { |
| 20 | initialize(); |
| 21 | } |
| 22 | |
| 23 | SessionData::~SessionData() { |
| 24 | } |
| 25 | |
| 26 | void SessionData::initialize() { |
| 27 | mWaitingOnCommand = false; |
| 28 | mSessionIsActive = false; |
| 29 | mLocalCapture = false; |
| 30 | mOneShot = false; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 31 | mSentSummary = false; |
| 32 | const size_t cpuIdSize = sizeof(int)*NR_CPUS; |
| 33 | // Share mCpuIds across all instances of gatord |
| 34 | mCpuIds = (int *)mmap(NULL, cpuIdSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 35 | if (mCpuIds == MAP_FAILED) { |
| 36 | logg->logError(__FILE__, __LINE__, "Unable to mmap shared memory for cpuids"); |
| 37 | handleException(); |
| 38 | } |
| 39 | memset(mCpuIds, -1, cpuIdSize); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 40 | readCpuInfo(); |
| 41 | mConfigurationXMLPath = NULL; |
| 42 | mSessionXMLPath = NULL; |
| 43 | mEventsXMLPath = NULL; |
| 44 | mTargetPath = NULL; |
| 45 | mAPCDir = NULL; |
| 46 | mSampleRate = 0; |
| 47 | mLiveRate = 0; |
| 48 | mDuration = 0; |
| 49 | mBacktraceDepth = 0; |
| 50 | mTotalBufferSize = 0; |
| 51 | // sysconf(_SC_NPROCESSORS_CONF) is unreliable on 2.6 Android, get the value from the kernel module |
| 52 | mCores = 1; |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 53 | mPageSize = 0; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SessionData::parseSessionXML(char* xmlString) { |
| 57 | SessionXML session(xmlString); |
| 58 | session.parse(); |
| 59 | |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 60 | // Set session data values - use prime numbers just below the desired value to reduce the chance of events firing at the same time |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 61 | if (strcmp(session.parameters.sample_rate, "high") == 0) { |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 62 | mSampleRate = 9973; // 10000 |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 63 | } else if (strcmp(session.parameters.sample_rate, "normal") == 0) { |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 64 | mSampleRate = 997; // 1000 |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 65 | } else if (strcmp(session.parameters.sample_rate, "low") == 0) { |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 66 | mSampleRate = 97; // 100 |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 67 | } else if (strcmp(session.parameters.sample_rate, "none") == 0) { |
| 68 | mSampleRate = 0; |
| 69 | } else { |
| 70 | logg->logError(__FILE__, __LINE__, "Invalid sample rate (%s) in session xml.", session.parameters.sample_rate); |
| 71 | handleException(); |
| 72 | } |
| 73 | mBacktraceDepth = session.parameters.call_stack_unwinding == true ? 128 : 0; |
| 74 | mDuration = session.parameters.duration; |
| 75 | |
| 76 | // Determine buffer size (in MB) based on buffer mode |
| 77 | mOneShot = true; |
| 78 | if (strcmp(session.parameters.buffer_mode, "streaming") == 0) { |
| 79 | mOneShot = false; |
| 80 | mTotalBufferSize = 1; |
| 81 | } else if (strcmp(session.parameters.buffer_mode, "small") == 0) { |
| 82 | mTotalBufferSize = 1; |
| 83 | } else if (strcmp(session.parameters.buffer_mode, "normal") == 0) { |
| 84 | mTotalBufferSize = 4; |
| 85 | } else if (strcmp(session.parameters.buffer_mode, "large") == 0) { |
| 86 | mTotalBufferSize = 16; |
| 87 | } else { |
| 88 | logg->logError(__FILE__, __LINE__, "Invalid value for buffer mode in session xml."); |
| 89 | handleException(); |
| 90 | } |
| 91 | |
| 92 | mImages = session.parameters.images; |
| 93 | // Convert milli- to nanoseconds |
| 94 | mLiveRate = session.parameters.live_rate * (int64_t)1000000; |
| 95 | if (mLiveRate > 0 && mLocalCapture) { |
| 96 | logg->logMessage("Local capture is not compatable with live, disabling live"); |
| 97 | mLiveRate = 0; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void SessionData::readCpuInfo() { |
| 102 | char temp[256]; // arbitrarily large amount |
| 103 | strcpy(mCoreName, "unknown"); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 104 | mMaxCpuId = -1; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 105 | |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 106 | FILE* f = fopen("/proc/cpuinfo", "r"); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 107 | if (f == NULL) { |
| 108 | logg->logMessage("Error opening /proc/cpuinfo\n" |
| 109 | "The core name in the captured xml file will be 'unknown'."); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | bool foundCoreName = false; |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 114 | int processor = -1; |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 115 | while (fgets(temp, sizeof(temp), f)) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 116 | const size_t len = strlen(temp); |
| 117 | |
| 118 | if (len == 1) { |
| 119 | // New section, clear the processor. Streamline will not know the cpus if the pre Linux 3.8 format of cpuinfo is encountered but also that no incorrect information will be transmitted. |
| 120 | processor = -1; |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | if (len > 0) { |
| 125 | temp[len - 1] = '\0'; // Replace the line feed with a null |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const bool foundHardware = strstr(temp, "Hardware") != 0; |
| 129 | const bool foundCPUPart = strstr(temp, "CPU part") != 0; |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 130 | const bool foundProcessor = strstr(temp, "processor") != 0; |
| 131 | if (foundHardware || foundCPUPart || foundProcessor) { |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 132 | char* position = strchr(temp, ':'); |
| 133 | if (position == NULL || (unsigned int)(position - temp) + 2 >= strlen(temp)) { |
| 134 | logg->logMessage("Unknown format of /proc/cpuinfo\n" |
| 135 | "The core name in the captured xml file will be 'unknown'."); |
| 136 | return; |
| 137 | } |
| 138 | position += 2; |
| 139 | |
| 140 | if (foundHardware) { |
| 141 | strncpy(mCoreName, position, sizeof(mCoreName)); |
| 142 | mCoreName[sizeof(mCoreName) - 1] = 0; // strncpy does not guarantee a null-terminated string |
| 143 | foundCoreName = true; |
| 144 | } |
| 145 | |
| 146 | if (foundCPUPart) { |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 147 | const int cpuId = strtol(position, NULL, 0); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 148 | // If this does not have the full topology in /proc/cpuinfo, mCpuIds[0] may not have the 1 CPU part emitted - this guarantees it's in mMaxCpuId |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 149 | if (cpuId > mMaxCpuId) { |
| 150 | mMaxCpuId = cpuId; |
| 151 | } |
| 152 | if (processor >= NR_CPUS) { |
| 153 | logg->logMessage("Too many processors, please increase NR_CPUS"); |
| 154 | } else if (processor >= 0) { |
| 155 | mCpuIds[processor] = cpuId; |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 156 | } |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (foundProcessor) { |
| 160 | processor = strtol(position, NULL, 0); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (!foundCoreName) { |
| 166 | logg->logMessage("Could not determine core name from /proc/cpuinfo\n" |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 167 | "The core name in the captured xml file will be 'unknown'."); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 168 | } |
| 169 | fclose(f); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 170 | } |
| 171 | |
| 172 | uint64_t getTime() { |
| 173 | struct timespec ts; |
| 174 | #ifndef CLOCK_MONOTONIC_RAW |
| 175 | // Android doesn't have this defined but it was added in Linux 2.6.28 |
| 176 | #define CLOCK_MONOTONIC_RAW 4 |
| 177 | #endif |
| 178 | if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) { |
| 179 | logg->logError(__FILE__, __LINE__, "Failed to get uptime"); |
| 180 | handleException(); |
| 181 | } |
| 182 | return (NS_PER_S*ts.tv_sec + ts.tv_nsec); |
| 183 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 184 | |
| 185 | int getEventKey() { |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 186 | // key 0 is reserved as a timestamp |
| 187 | // key 1 is reserved as the marker for thread specific counters |
| 188 | // Odd keys are assigned by the driver, even keys by the daemon |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 189 | static int key = 2; |
| 190 | |
| 191 | const int ret = key; |
| 192 | key += 2; |
| 193 | return ret; |
| 194 | } |