Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 2 | * Copyright (C) ARM Limited 2013-2015. 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 | |
| 9 | #ifndef BUFFER_H |
| 10 | #define BUFFER_H |
| 11 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | #include <semaphore.h> |
| 14 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 15 | #include "k/perf_event.h" |
| 16 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 17 | class Sender; |
| 18 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 19 | enum { |
| 20 | FRAME_SUMMARY = 1, |
| 21 | FRAME_BLOCK_COUNTER = 5, |
| 22 | FRAME_EXTERNAL = 10, |
| 23 | FRAME_PERF_ATTRS = 11, |
| 24 | FRAME_PERF = 12, |
| 25 | }; |
| 26 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 27 | class Buffer { |
| 28 | public: |
| 29 | static const size_t MAXSIZE_PACK32 = 5; |
| 30 | static const size_t MAXSIZE_PACK64 = 10; |
| 31 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 32 | Buffer(int32_t core, int32_t buftype, const int size, sem_t *const readerSem); |
| 33 | ~Buffer(); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 34 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 35 | void write(Sender *sender); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 36 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 37 | int bytesAvailable() const; |
| 38 | int contiguousSpaceAvailable() const; |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 39 | void commit(const uint64_t time, const bool force = false); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 40 | void check(const uint64_t time); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 41 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 42 | // Summary messages |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 43 | void summary(const uint64_t currTime, const int64_t timestamp, const int64_t uptime, const int64_t monotonicDelta, const char *const uname); |
| 44 | void coreName(const uint64_t currTime, const int core, const int cpuid, const char *const name); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 45 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 46 | // Block Counter messages |
| 47 | bool eventHeader(uint64_t curr_time); |
| 48 | bool eventTid(int tid); |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 49 | void event(int key, int32_t value); |
| 50 | void event64(int key, int64_t value); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 51 | |
| 52 | // Perf Attrs messages |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 53 | void marshalPea(const uint64_t currTime, const struct perf_event_attr *const pea, int key); |
| 54 | void marshalKeys(const uint64_t currTime, const int count, const __u64 *const ids, const int *const keys); |
| 55 | void marshalKeysOld(const uint64_t currTime, const int keyCount, const int *const keys, const int bytes, const char *const buf); |
| 56 | void marshalFormat(const uint64_t currTime, const int length, const char *const format); |
| 57 | void marshalMaps(const uint64_t currTime, const int pid, const int tid, const char *const maps); |
| 58 | void marshalComm(const uint64_t currTime, const int pid, const int tid, const char *const image, const char *const comm); |
| 59 | void onlineCPU(const uint64_t currTime, const int cpu); |
| 60 | void offlineCPU(const uint64_t currTime, const int cpu); |
| 61 | void marshalKallsyms(const uint64_t currTime, const char *const kallsyms); |
| 62 | void perfCounterHeader(const uint64_t time); |
| 63 | void perfCounter(const int core, const int key, const int64_t value); |
| 64 | void perfCounterFooter(const uint64_t currTime); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 65 | |
| 66 | void setDone(); |
| 67 | bool isDone() const; |
| 68 | |
| 69 | // Prefer a new member to using these functions if possible |
| 70 | char *getWritePos() { return mBuf + mWritePos; } |
| 71 | void advanceWrite(int bytes) { mWritePos = (mWritePos + bytes) & /*mask*/(mSize - 1); } |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 72 | static void packInt(char *const buf, const int size, int &writePos, int32_t x); |
| 73 | void packInt(int32_t x); |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 74 | static void packInt64(char *const buf, const int size, int &writePos, int64_t x); |
Jon Medhurst | e31266f | 2014-08-04 15:47:44 +0100 | [diff] [blame] | 75 | void packInt64(int64_t x); |
| 76 | void writeBytes(const void *const data, size_t count); |
| 77 | void writeString(const char *const str); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 78 | |
| 79 | static void writeLEInt(unsigned char *buf, int v) { |
| 80 | buf[0] = (v >> 0) & 0xFF; |
| 81 | buf[1] = (v >> 8) & 0xFF; |
| 82 | buf[2] = (v >> 16) & 0xFF; |
| 83 | buf[3] = (v >> 24) & 0xFF; |
| 84 | } |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 85 | |
| 86 | private: |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 87 | void frame(); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 88 | bool commitReady() const; |
| 89 | bool checkSpace(int bytes); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 90 | |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 91 | char *const mBuf; |
| 92 | sem_t *const mReaderSem; |
| 93 | uint64_t mCommitTime; |
| 94 | sem_t mWriterSem; |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 95 | const int mSize; |
| 96 | int mReadPos; |
| 97 | int mWritePos; |
| 98 | int mCommitPos; |
| 99 | bool mAvailable; |
| 100 | bool mIsDone; |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 101 | const int32_t mCore; |
| 102 | const int32_t mBufType; |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 103 | |
| 104 | // Intentionally unimplemented |
| 105 | Buffer(const Buffer &); |
| 106 | Buffer &operator=(const Buffer &); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | #endif // BUFFER_H |