gator: Version 5.20
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / Buffer.h
1 /**
2  * Copyright (C) ARM Limited 2013-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 #ifndef BUFFER_H
10 #define BUFFER_H
11
12 #include <stdint.h>
13 #include <semaphore.h>
14
15 #include "k/perf_event.h"
16
17 class Sender;
18
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
27 class Buffer {
28 public:
29         static const size_t MAXSIZE_PACK32 = 5;
30         static const size_t MAXSIZE_PACK64 = 10;
31
32         Buffer(int32_t core, int32_t buftype, const int size, sem_t *const readerSem);
33         ~Buffer();
34
35         void write(Sender *sender);
36
37         int bytesAvailable() const;
38         int contiguousSpaceAvailable() const;
39         void commit(const uint64_t time);
40         void check(const uint64_t time);
41
42         // Summary messages
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);
45
46         // Block Counter messages
47         bool eventHeader(uint64_t curr_time);
48         bool eventTid(int tid);
49         void event(int key, int32_t value);
50         void event64(int key, int64_t value);
51
52         // Perf Attrs messages
53         void pea(const uint64_t currTime, const struct perf_event_attr *const pea, int key);
54         void keys(const uint64_t currTime, const int count, const __u64 *const ids, const int *const keys);
55         void keysOld(const uint64_t currTime, const int keyCount, const int *const keys, const int bytes, const char *const buf);
56         void format(const uint64_t currTime, const int length, const char *const format);
57         void maps(const uint64_t currTime, const int pid, const int tid, const char *const maps);
58         void comm(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 uint64_t time, const int cpu);
60         void offlineCPU(const uint64_t currTime, const uint64_t time, const int cpu);
61         void kallsyms(const uint64_t currTime, const char *const kallsyms);
62
63         void setDone();
64         bool isDone() const;
65
66         // Prefer a new member to using these functions if possible
67         char *getWritePos() { return mBuf + mWritePos; }
68         void advanceWrite(int bytes) { mWritePos = (mWritePos + bytes) & /*mask*/(mSize - 1); }
69         static void packInt(char *const buf, const int size, int &writePos, int32_t x);
70         void packInt(int32_t x);
71         static void packInt64(char *const buf, const int size, int &writePos, int64_t x);
72         void packInt64(int64_t x);
73         void writeBytes(const void *const data, size_t count);
74         void writeString(const char *const str);
75
76         static void writeLEInt(unsigned char *buf, int v) {
77                 buf[0] = (v >> 0) & 0xFF;
78                 buf[1] = (v >> 8) & 0xFF;
79                 buf[2] = (v >> 16) & 0xFF;
80                 buf[3] = (v >> 24) & 0xFF;
81         }
82
83 private:
84         void frame();
85         bool commitReady() const;
86         bool checkSpace(int bytes);
87
88         char *const mBuf;
89         sem_t *const mReaderSem;
90         uint64_t mCommitTime;
91         sem_t mWriterSem;
92         const int mSize;
93         int mReadPos;
94         int mWritePos;
95         int mCommitPos;
96         bool mAvailable;
97         bool mIsDone;
98         const int32_t mCore;
99         const int32_t mBufType;
100
101         // Intentionally unimplemented
102         Buffer(const Buffer &);
103         Buffer &operator=(const Buffer &);
104 };
105
106 #endif // BUFFER_H