blob: 2de1b97ac091f92040485489818b337d81104359 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/**
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
17class Sender;
18
19enum {
20 FRAME_SUMMARY = 1,
21 FRAME_BLOCK_COUNTER = 5,
22 FRAME_EXTERNAL = 10,
23 FRAME_PERF_ATTRS = 11,
24 FRAME_PERF = 12,
25};
26
27class Buffer {
28public:
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 void frame();
43
44 // Summary messages
45 void summary(const int64_t timestamp, const int64_t uptime, const int64_t monotonicDelta, const char *const uname);
46 void coreName(const int core, const int cpuid, const char *const name);
47
48 // Block Counter messages
49 bool eventHeader(uint64_t curr_time);
50 bool eventTid(int tid);
51 void event(int32_t key, int32_t value);
52 void event64(int64_t key, int64_t value);
53
54 // Perf Attrs messages
55 void pea(const struct perf_event_attr *const pea, int key);
56 void keys(const int count, const __u64 *const ids, const int *const keys);
57 void keysOld(const int keyCount, const int *const keys, const int bytes, const char *const buf);
58 void format(const int length, const char *const format);
59 void maps(const int pid, const int tid, const char *const maps);
60 void comm(const int pid, const int tid, const char *const image, const char *const comm);
61
62 void setDone();
63 bool isDone() const;
64
65 // Prefer a new member to using these functions if possible
66 char *getWritePos() { return mBuf + mWritePos; }
67 void advanceWrite(int bytes) { mWritePos = (mWritePos + bytes) & /*mask*/(mSize - 1); }
68 static void packInt(char *const buf, const int size, int &writePos, int32_t x);
69 void packInt(int32_t x);
70 void packInt64(int64_t x);
71 void writeBytes(const void *const data, size_t count);
72 void writeString(const char *const str);
73
74 static void writeLEInt(unsigned char *buf, int v) {
75 buf[0] = (v >> 0) & 0xFF;
76 buf[1] = (v >> 8) & 0xFF;
77 buf[2] = (v >> 16) & 0xFF;
78 buf[3] = (v >> 24) & 0xFF;
79 }
80
81private:
82 bool commitReady() const;
83 bool checkSpace(int bytes);
84
85 const int32_t mCore;
86 const int32_t mBufType;
87 const int mSize;
88 int mReadPos;
89 int mWritePos;
90 int mCommitPos;
91 bool mAvailable;
92 bool mIsDone;
93 char *const mBuf;
94 uint64_t mCommitTime;
95 sem_t *const mReaderSem;
96
97 // Intentionally unimplemented
98 Buffer(const Buffer &);
99 Buffer &operator=(const Buffer &);
100};
101
102#endif // BUFFER_H