Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright (C) ARM Limited 2014. 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 "MaliVideoDriver.h" |
| 10 | |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include "Buffer.h" |
| 14 | #include "Counter.h" |
| 15 | #include "Logging.h" |
| 16 | #include "SessionData.h" |
| 17 | |
| 18 | // From instr/src/mve_instr_comm_protocol.h |
| 19 | typedef enum mve_instr_configuration_type { |
| 20 | MVE_INSTR_RAW = 1 << 0, |
| 21 | MVE_INSTR_COUNTERS = 1 << 1, |
| 22 | MVE_INSTR_EVENTS = 1 << 2, |
| 23 | MVE_INSTR_ACTIVITIES = 1 << 3, |
| 24 | |
| 25 | // Raw always pushed regardless |
| 26 | MVE_INSTR_PULL = 1 << 12, |
| 27 | // Raw always unpacked regardless |
| 28 | MVE_INSTR_PACKED_COMM = 1 << 13, |
| 29 | // Don’t send ACKt response |
| 30 | MVE_INSTR_NO_AUTO_ACK = 1 << 14, |
| 31 | } mve_instr_configuration_type_t; |
| 32 | |
| 33 | static const char COUNTER[] = "ARM_Mali-V500_cnt"; |
| 34 | static const char EVENT[] = "ARM_Mali-V500_evn"; |
| 35 | static const char ACTIVITY[] = "ARM_Mali-V500_act"; |
| 36 | |
| 37 | class MaliVideoCounter { |
| 38 | public: |
| 39 | MaliVideoCounter(MaliVideoCounter *next, const char *name, const MaliVideoCounterType type, const int id) : mNext(next), mName(name), mType(type), mId(id), mKey(getEventKey()), mEnabled(false) { |
| 40 | } |
| 41 | |
| 42 | ~MaliVideoCounter() { |
| 43 | delete mName; |
| 44 | } |
| 45 | |
| 46 | MaliVideoCounter *getNext() const { return mNext; } |
| 47 | const char *getName() const { return mName; } |
| 48 | MaliVideoCounterType getType() const { return mType; } |
| 49 | int getId() const { return mId; } |
| 50 | int getKey() const { return mKey; } |
| 51 | bool isEnabled() const { return mEnabled; } |
| 52 | void setEnabled(const bool enabled) { mEnabled = enabled; } |
| 53 | |
| 54 | private: |
| 55 | MaliVideoCounter *const mNext; |
| 56 | const char *const mName; |
| 57 | const MaliVideoCounterType mType; |
| 58 | // Mali Video id |
| 59 | const int mId; |
| 60 | // Streamline key |
| 61 | const int mKey; |
| 62 | bool mEnabled; |
| 63 | }; |
| 64 | |
| 65 | MaliVideoDriver::MaliVideoDriver() : mCounters(NULL), mActivityCount(0) { |
| 66 | } |
| 67 | |
| 68 | MaliVideoDriver::~MaliVideoDriver() { |
| 69 | while (mCounters != NULL) { |
| 70 | MaliVideoCounter *counter = mCounters; |
| 71 | mCounters = counter->getNext(); |
| 72 | delete counter; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void MaliVideoDriver::setup(mxml_node_t *const xml) { |
| 77 | // hwmon does not currently work with perf |
| 78 | if (gSessionData->perf.isSetup()) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | mxml_node_t *node = xml; |
| 83 | while (true) { |
| 84 | node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND); |
| 85 | if (node == NULL) { |
| 86 | break; |
| 87 | } |
| 88 | const char *counter = mxmlElementGetAttr(node, "counter"); |
| 89 | if (counter == NULL) { |
| 90 | // Ignore |
| 91 | } else if (strncmp(counter, COUNTER, sizeof(COUNTER) - 1) == 0) { |
| 92 | const int i = strtol(counter + sizeof(COUNTER) - 1, NULL, 10); |
| 93 | mCounters = new MaliVideoCounter(mCounters, strdup(counter), MVCT_COUNTER, i); |
| 94 | } else if (strncmp(counter, EVENT, sizeof(EVENT) - 1) == 0) { |
| 95 | const int i = strtol(counter + sizeof(EVENT) - 1, NULL, 10); |
| 96 | mCounters = new MaliVideoCounter(mCounters, strdup(counter), MVCT_EVENT, i); |
| 97 | } else if (strcmp(counter, ACTIVITY) == 0) { |
| 98 | mCounters = new MaliVideoCounter(mCounters, strdup(ACTIVITY), MVCT_ACTIVITY, 0); |
| 99 | mActivityCount = 0; |
| 100 | while (true) { |
| 101 | char buf[32]; |
| 102 | snprintf(buf, sizeof(buf), "activity%i", mActivityCount + 1); |
| 103 | if (mxmlElementGetAttr(node, buf) == NULL) { |
| 104 | break; |
| 105 | } |
| 106 | ++mActivityCount; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | MaliVideoCounter *MaliVideoDriver::findCounter(const Counter &counter) const { |
| 113 | for (MaliVideoCounter *maliVideoCounter = mCounters; maliVideoCounter != NULL; maliVideoCounter = maliVideoCounter->getNext()) { |
| 114 | if (strcmp(maliVideoCounter->getName(), counter.getType()) == 0) { |
| 115 | return maliVideoCounter; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | bool MaliVideoDriver::claimCounter(const Counter &counter) const { |
| 123 | return findCounter(counter) != NULL; |
| 124 | } |
| 125 | |
| 126 | bool MaliVideoDriver::countersEnabled() const { |
| 127 | for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) { |
| 128 | if (counter->isEnabled()) { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void MaliVideoDriver::resetCounters() { |
| 136 | for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) { |
| 137 | counter->setEnabled(false); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void MaliVideoDriver::setupCounter(Counter &counter) { |
| 142 | MaliVideoCounter *const maliVideoCounter = findCounter(counter); |
| 143 | if (maliVideoCounter == NULL) { |
| 144 | counter.setEnabled(false); |
| 145 | return; |
| 146 | } |
| 147 | maliVideoCounter->setEnabled(true); |
| 148 | counter.setKey(maliVideoCounter->getKey()); |
| 149 | } |
| 150 | |
| 151 | int MaliVideoDriver::writeCounters(mxml_node_t *root) const { |
| 152 | if (access("/dev/mv500", F_OK) != 0) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int count = 0; |
| 157 | for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) { |
| 158 | mxml_node_t *node = mxmlNewElement(root, "counter"); |
| 159 | mxmlElementSetAttr(node, "name", counter->getName()); |
| 160 | ++count; |
| 161 | } |
| 162 | |
| 163 | return count; |
| 164 | } |
| 165 | |
| 166 | void MaliVideoDriver::marshalEnable(const MaliVideoCounterType type, char *const buf, const size_t bufsize, int &pos) { |
| 167 | // size |
| 168 | int numEnabled = 0; |
| 169 | for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) { |
| 170 | if (counter->isEnabled() && (counter->getType() == type)) { |
| 171 | ++numEnabled; |
| 172 | } |
| 173 | } |
| 174 | Buffer::packInt(buf, bufsize, pos, numEnabled*sizeof(uint32_t)); |
| 175 | for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) { |
| 176 | if (counter->isEnabled() && (counter->getType() == type)) { |
| 177 | Buffer::packInt(buf, bufsize, pos, counter->getId()); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | bool MaliVideoDriver::start(const int mveUds) { |
| 183 | char buf[256]; |
| 184 | int pos = 0; |
| 185 | |
| 186 | // code - MVE_INSTR_STARTUP |
| 187 | buf[pos++] = 'C'; |
| 188 | buf[pos++] = 'L'; |
| 189 | buf[pos++] = 'N'; |
| 190 | buf[pos++] = 'T'; |
| 191 | // size |
| 192 | Buffer::packInt(buf, sizeof(buf), pos, sizeof(uint32_t)); |
| 193 | // client_version_number |
| 194 | Buffer::packInt(buf, sizeof(buf), pos, 1); |
| 195 | |
| 196 | // code - MVE_INSTR_CONFIGURE |
| 197 | buf[pos++] = 'C'; |
| 198 | buf[pos++] = 'N'; |
| 199 | buf[pos++] = 'F'; |
| 200 | buf[pos++] = 'G'; |
| 201 | // size |
| 202 | Buffer::packInt(buf, sizeof(buf), pos, 5*sizeof(uint32_t)); |
| 203 | // configuration |
| 204 | Buffer::packInt(buf, sizeof(buf), pos, MVE_INSTR_COUNTERS | MVE_INSTR_EVENTS | MVE_INSTR_ACTIVITIES | MVE_INSTR_PACKED_COMM); |
| 205 | // communication_protocol_version |
| 206 | Buffer::packInt(buf, sizeof(buf), pos, 1); |
| 207 | // data_protocol_version |
| 208 | Buffer::packInt(buf, sizeof(buf), pos, 1); |
| 209 | // sample_rate - convert samples/second to ms/sample |
| 210 | Buffer::packInt(buf, sizeof(buf), pos, 1000/gSessionData->mSampleRate); |
| 211 | // live_rate - convert ns/flush to ms/flush |
| 212 | Buffer::packInt(buf, sizeof(buf), pos, gSessionData->mLiveRate/1000000); |
| 213 | |
| 214 | // code - MVE_INSTR_ENABLE_COUNTERS |
| 215 | buf[pos++] = 'C'; |
| 216 | buf[pos++] = 'F'; |
| 217 | buf[pos++] = 'G'; |
| 218 | buf[pos++] = 'c'; |
| 219 | marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos); |
| 220 | |
| 221 | // code - MVE_INSTR_ENABLE_EVENTS |
| 222 | buf[pos++] = 'C'; |
| 223 | buf[pos++] = 'F'; |
| 224 | buf[pos++] = 'G'; |
| 225 | buf[pos++] = 'e'; |
| 226 | marshalEnable(MVCT_EVENT, buf, sizeof(buf), pos); |
| 227 | |
| 228 | /* |
| 229 | // code - MVE_INSTR_ENABLE_ACTIVITIES |
| 230 | buf[pos++] = 'C'; |
| 231 | buf[pos++] = 'F'; |
| 232 | buf[pos++] = 'G'; |
| 233 | buf[pos++] = 'a'; |
| 234 | // size |
| 235 | Buffer::packInt(buf, sizeof(buf), pos, mActivityCount*sizeof(uint32_t)); |
| 236 | for (int i = 0; i < mActivityCount; ++i) { |
| 237 | // activity_id |
| 238 | Buffer::packInt(buf, sizeof(buf), pos, i); |
| 239 | } |
| 240 | */ |
| 241 | |
| 242 | int written = 0; |
| 243 | while (written < pos) { |
| 244 | size_t bytes = ::write(mveUds, buf + written, pos - written); |
| 245 | if (bytes <= 0) { |
| 246 | logg->logMessage("%s(%s:%i): write failed", __FUNCTION__, __FILE__, __LINE__); |
| 247 | return false; |
| 248 | } |
| 249 | written += bytes; |
| 250 | } |
| 251 | |
| 252 | return true; |
| 253 | } |