Skip to content

Commit d7f0712

Browse files
KaigeFulijinxia
authored andcommitted
tools: acrntrace: Add option -t for max capture time
If option -t is configured, a timer will be set with configured timeout when launching acrntrace. When timer fired, acrntrace will exit. Signed-off-by: Kaige Fu <kaige.fu@intel.com> Reviewed-by: Eddie Dong <eddie.dong@intel.com> Reviewed-by: Geoffroy Van Cutsem <geoffroy.vancutsem@intel.com>
1 parent 5042ba6 commit d7f0712

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

tools/acrntrace/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
OUT_DIR ?= .
33

44
all:
5-
$(CC) -o $(OUT_DIR)/acrntrace acrntrace.c sbuf.c -I. -lpthread
5+
$(CC) -o $(OUT_DIR)/acrntrace acrntrace.c sbuf.c -I. -lpthread -lrt
66

77
clean:
88
rm -f $(OUT_DIR)/acrntrace

tools/acrntrace/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Options:
1919

2020
-h print this message
2121
-i period specify polling interval in milliseconds [1-999]
22+
-t max time to capture trace data (in second)
2223
-c clear the buffered old data
2324
-r minimal amount (in MB) of free space kept on the disk
2425
before acrntrace stops

tools/acrntrace/acrntrace.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@
1515
#include <errno.h>
1616
#include <pthread.h>
1717
#include <string.h>
18+
#include <signal.h>
1819

1920
#include "acrntrace.h"
2021

2122
/* default minimal amount free space (in MB) left on the disk */
2223
static uint64_t disk_reserved = 512;
24+
25+
#define TIMER_ID (128)
26+
static uint32_t timeout = 0;
27+
2328
static int exiting = 0;
2429

2530
/* for opt */
2631
static uint64_t period = 10000;
27-
static const char optString[] = "i:hcr:";
32+
static const char optString[] = "i:hcr:t:";
2833
static const char dev_name[] = "/dev/acrn_trace";
2934

3035
static uint32_t flags;
@@ -42,9 +47,50 @@ static void display_usage(void)
4247
"\t-r: minimal amount (in MB) of free space kept on the disk\n"
4348
"\t before acrntrace stops\n"
4449
"\t-i: period_in_ms: specify polling interval [1-999]\n"
50+
"\t-t: max time to capture trace data (in second)\n"
4551
"\t-c: clear the buffered old data\n");
4652
}
4753

54+
static void timer_handler(union sigval sv)
55+
{
56+
exiting = 1;
57+
exit(0);
58+
}
59+
60+
static int init_timer(int timeout)
61+
{
62+
struct sigevent event;
63+
struct itimerspec it;
64+
timer_t tm_id;
65+
int err;
66+
67+
memset(&event, 0, sizeof(struct sigevent));
68+
69+
event.sigev_value.sival_int = TIMER_ID;
70+
event.sigev_notify = SIGEV_THREAD;
71+
event.sigev_notify_function = timer_handler;
72+
73+
err = timer_create(CLOCK_MONOTONIC, &event, &tm_id);
74+
if (err < 0) {
75+
pr_err("Error to create timer, errno(%d)\n", err);
76+
return err;
77+
}
78+
79+
it.it_interval.tv_sec = timeout;
80+
it.it_interval.tv_nsec = 0;
81+
it.it_value.tv_sec = timeout;
82+
it.it_value.tv_nsec = 0;
83+
84+
err = timer_settime(tm_id, 0, &it, NULL);
85+
if (err < 0) {
86+
pr_err("Error to set timer, errno(%d)\n", err);
87+
return err;
88+
}
89+
90+
pr_info("Capture trace data for about %ds and exit\n", timeout);
91+
return 0;
92+
}
93+
4894
static int parse_opt(int argc, char *argv[])
4995
{
5096
int opt, ret;
@@ -69,6 +115,15 @@ static int parse_opt(int argc, char *argv[])
69115
disk_reserved = ret;
70116
pr_dbg("Keeping %dMB of space on the disk\n", ret);
71117
break;
118+
case 't':
119+
ret = atoi(optarg);
120+
if (ret <= 0) {
121+
pr_err("'-t' require integer greater than 0\n");
122+
return -EINVAL;
123+
}
124+
timeout = ret;
125+
pr_dbg("Capture trace data for at most %ds\n", ret);
126+
break;
72127
case 'c':
73128
flags |= FLAG_CLEAR_BUF;
74129
break;
@@ -331,6 +386,7 @@ static void signal_exit_handler(int sig)
331386
int main(int argc, char *argv[])
332387
{
333388
uint32_t cpu = 0;
389+
int err;
334390

335391
/* parse options */
336392
if (parse_opt(argc, argv))
@@ -350,6 +406,15 @@ int main(int argc, char *argv[])
350406
exit(EXIT_FAILURE);
351407
}
352408

409+
/* Set timer if timeout configured */
410+
if (timeout) {
411+
err = init_timer(timeout);
412+
if (err < 0) {
413+
pr_err("Failed to set timer\n");
414+
exit(EXIT_FAILURE);
415+
}
416+
}
417+
353418
atexit(handle_on_exit);
354419

355420
/* acquair res for each trace dev */

0 commit comments

Comments
 (0)