1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef __PERF_COUNTER_H__
17 #define __PERF_COUNTER_H__
18 #include <cstdlib>
19 #include <unistd.h>
20
21 constexpr int RECORD_NUM = 1024 * 32 * 16;
22 constexpr int NAME_LEN = 32;
23
24 constexpr int TASK_NUM = 1;
25
26 constexpr int MAX_COUNTERS = 7;
27
28 struct counters_t {
29 unsigned long nr;
30 unsigned long vals[MAX_COUNTERS];
31 };
32
33 struct perf_record_t {
34 char name[NAME_LEN];
35 short begin_flag;
36 short end_flag;
37 struct counters_t counters_begin;
38 struct counters_t counters_end;
39 };
40
41 struct perf_task_t {
42 unsigned long rd;
43
44 struct perf_record_t perf_record[RECORD_NUM];
45 };
46
47 struct perf_stat_t {
48 int perf_fd;
49 int n_counters;
50 pid_t pid;
51 int rsvd;
52 struct perf_task_t perf_task[TASK_NUM];
53 };
54
55 #define CPU_CYCLES 0x11
56 #define INST_RETIRED 0x08
57 #define INST_SPEC 0x1b
58 #define BR_RETIRED 0x21
59 #define BR_MIS_PRED_RETIRED 0x22
60 #define STALL_FRONTEND 0x23
61 #define STALL_BACKEND 0x24
62
63 #ifndef _MSC_VER
64 unsigned long perf_begin(const char* name, int id);
65 void perf_end(int id, unsigned long rd);
66
67 void perf_counter_output_all(void);
68 void perf_counter_output_single(void);
69 void perf_counter_clear(void);
70 #else
perf_begin(const char * name,int id)71 static unsigned long perf_begin(const char* name, int id)
72 {
73 return 0;
74 };
perf_end(int id,unsigned long rd)75 static void perf_end(int id, unsigned long rd) {};
76
perf_counter_output_all(void)77 static void perf_counter_output_all(void) {};
perf_counter_output_single(void)78 static void perf_counter_output_single(void) {};
perf_counter_clear(void)79 static void perf_counter_clear(void) {};
80 #endif
81 #endif
82