summaryrefslogtreecommitdiff
path: root/test/test_perfmon_cpu.c
blob: 8ecb241abc673a90db9631e824fb7cfae5902b75 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (c) 2014 Remy Noel.
 * Copyright (c) 2014 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Here, we test the perfmon module for cross CPU performances monitoring.
 */

#include <stdbool.h>
#include <stddef.h>

#include <kern/error.h>
#include <kern/perfmon_i.h>
#include <kern/printf.h>
#include <kern/thread.h>
#include <test/test.h>

#define WAIT_DELAY_USEC 1000000

static volatile bool stop;

static void
test_do_nothing(void* arg)
{
    (void)arg;

    while (!stop);
}

static void
test_report_event(const struct perfmon_event *event, const char *name)
{
    unsigned long long count;

    count = perfmon_event_read(event);
    printf("test: %s: %llu\n", name, count);
}

static void
test_run(void *arg)
{
    struct perfmon_event *ev_cycle, *ev_instruction;
    struct perfmon_group *group;
    int error;
    uint64_t pmc_max;

    (void)arg;

    pmc_max = (1 << perfmon_get_pmc_width()) - 1;
    error = perfmon_group_create(&group);
    error_check(error, "perfmon_group_create");

    error = perfmon_event_create(&ev_cycle, PERFMON_ET_GENERIC,
                                 PERFMON_EV_CYCLE, PERFMON_EF_KERN);
    error_check(error, "perfmon_event_create");
    perfmon_group_add(group, ev_cycle);

    error = perfmon_event_create(&ev_instruction, PERFMON_ET_GENERIC,
                                 PERFMON_EV_INSTRUCTION, PERFMON_EF_KERN);
    error_check(error, "perfmon_event_create");
    perfmon_group_add(group, ev_instruction);

    error = perfmon_group_attach_cpu(group, 1);
    error_check(error, "perfmon_group_attach_cpu 1");

    error = perfmon_group_start(group);
    error_check(error, "perfmon_group_start");

    cpu_delay(WAIT_DELAY_USEC);
    error = perfmon_group_stop(group);
    error_check(error, "perfmon_group_stop");
    test_report_event(ev_cycle, "cycle");
    test_report_event(ev_instruction, "instruction");

    printf("checking with overflow ...\n");
    /* TODO: choose value depending of architecture */
    perfmon_event_write(ev_cycle, pmc_max - perfmon_event_read(ev_cycle) / 2);
    perfmon_event_write(ev_instruction,
                        pmc_max - perfmon_event_read(ev_instruction) / 3);
    perfmon_event_reset(ev_cycle);
    perfmon_event_reset(ev_instruction);

    error = perfmon_group_start(group);
    error_check(error, "perfmon_group_start");

    cpu_delay(WAIT_DELAY_USEC);
    error = perfmon_group_stop(group);
    error_check(error, "perfmon_group_stop");
    test_report_event(ev_cycle, "cycle");
    test_report_event(ev_instruction, "instruction");

    error = perfmon_group_detach(group);
    error_check(error, "perfmon_group_detach");

    error = perfmon_group_destroy(group);
    error_check(error, "perfmon_group_destroy");

    stop = true;
}

void
test_setup(void)
{
    struct thread_attr attr;
    struct thread *thread0, *thread1;
    struct cpumap *cpumap;
    int error;

    error = cpumap_create(&cpumap);
    error_check(error, "cpumap_create 0");
    cpumap_zero(cpumap);
    cpumap_set(cpumap, 0);

    thread_attr_init(&attr, "x15_test_run");
    thread_attr_set_detached(&attr);
    thread_attr_set_cpumap(&attr, cpumap);
    error = thread_create(&thread0, &attr, test_run, NULL);
    error_check(error, "thread_create 0");

    cpumap_zero(cpumap);
    cpumap_set(cpumap, 1);

    thread_attr_init(&attr, "x15_test_do_nothing");
    thread_attr_set_detached(&attr);
    thread_attr_set_cpumap(&attr, cpumap);
    error = thread_create(&thread1, &attr, test_do_nothing, NULL);
    error_check(error, "thread_create 1");
}