-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
64 lines (58 loc) · 1.77 KB
/
Copy pathutils.c
File metadata and controls
64 lines (58 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 MoatLab, Virginia Tech. */
/* utils.c — small stateless utility functions.
* _GNU_SOURCE is supplied by the Makefile (-D_GNU_SOURCE); needed here for
* sched_setaffinity. */
#include <errno.h>
#include <sched.h>
#include <signal.h> /* kill */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* pid_t */
#include <time.h>
#include <unistd.h> /* usleep */
#include "pact.h" /* pac_metadata_t, pact_context_t */
#include "tsc.h" /* rdtsc */
#include "utils.h"
#include "error.h"
uint64_t pact_detect_tsc_frequency(void)
{
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
uint64_t tsc_start = rdtsc();
usleep(10000); /* 10 ms calibration window */
uint64_t tsc_end = rdtsc();
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t elapsed_ns =
(end.tv_sec - start.tv_sec) * 1000000000ULL + (end.tv_nsec - start.tv_nsec);
return (tsc_end - tsc_start) * 1000000000ULL / elapsed_ns;
}
int pact_pin_to_cpu(int cpu)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0) {
log_error("pin_to_cpu", "Failed to set affinity to CPU %d: %s", cpu, strerror(errno));
return -1;
}
log_info("pin_to_cpu", "Pinned to CPU %d", cpu);
return 0;
}
bool pact_check_all_targets_exited(pact_context_t *ctx)
{
pid_t pid = ctx->workload->target_pid;
if (pid <= 0) {
return true;
}
/* kill(pid, 0) probes process existence without sending a signal.
* EPERM = process exists but we cannot signal it — treat as alive. */
if (kill(pid, 0) == 0) {
return false;
}
if (errno == EPERM) {
return false;
}
return true;
}