forked from iovisor/ubpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ubpf_inst_cnt: Add instruction counting.
Add a new struct that can carry information about the various types of instruction counting we wish to perform. Embed this struct into the struct vm so it can be enabled without changing the prototypes for _exec etc. Add an set function so library users can enable instruction counting and get results. In order to count the instructions executed on the underlying bare-metal machine we leverage the perf_event framework (Linux only). This uses ioctls to initialize and then measure the relevant counters to ascertain how many instructions have been executing on the CPU. Note that access to these counters requires elevated privileges so that may limit who can run the program in this mode. Fixes iovisor#83.
- Loading branch information
1 parent
7c8be6a
commit 3b6bf79
Showing
5 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2021 Eideticom, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#define _GNU_SOURCE | ||
#include "ubpf_int.h" | ||
|
||
#include <unistd.h> | ||
#include <sys/syscall.h> | ||
#include <sys/ioctl.h> | ||
|
||
#ifdef __linux__ | ||
#include <linux/perf_event.h> | ||
|
||
void enable_instruction_count(int fd) | ||
{ | ||
ioctl(fd, PERF_EVENT_IOC_RESET, 0); | ||
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); | ||
} | ||
|
||
void disable_instruction_count(int fd) | ||
{ | ||
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); | ||
} | ||
|
||
int setup_instruction_counter(void) | ||
{ | ||
int fd; | ||
|
||
struct perf_event_attr pe = { | ||
.type = PERF_TYPE_HARDWARE, | ||
.size = sizeof(struct perf_event_attr), | ||
.config = PERF_COUNT_HW_INSTRUCTIONS, | ||
.disabled = 1, | ||
.exclude_kernel = 1, | ||
.exclude_hv = 1, | ||
}; | ||
|
||
fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0); | ||
|
||
return fd; | ||
} | ||
|
||
long long get_instruction_count(int fd) | ||
{ | ||
long long count; | ||
ssize_t rd; | ||
|
||
rd = read(fd, &count, sizeof(long long)); | ||
if (rd != sizeof(long long)) | ||
return -1; | ||
|
||
return count; | ||
} | ||
|
||
#else /* __linux__ */ | ||
|
||
#include <errno.h> | ||
|
||
void disable_instruction_count(int fd) | ||
{ | ||
(void)fd; | ||
} | ||
|
||
void enable_instruction_count(int fd) | ||
{ | ||
(void)fd; | ||
} | ||
|
||
int setup_instruction_counter(void) | ||
{ | ||
errno = ENOTSUP; | ||
return -1; | ||
} | ||
|
||
long long get_instruction_count(int fd) | ||
{ | ||
(void)fd; | ||
return -1; | ||
} | ||
|
||
#endif /* __linux__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters