File: hiccup.c

package info (click to toggle)
diskscan 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: ansic: 11,136; python: 338; xml: 138; sh: 41; makefile: 34
file content (207 lines) | stat: -rw-r--r-- 5,004 bytes parent folder | download | duplicates (6)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
 * hiccup.c
 * Written by Michael Barker and released to the public domain,
 * as explained at http://creativecommons.org/publicdomain/zero/1.0/
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/timerfd.h>
#include <poll.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <hdr_histogram.h>
#include <hdr_histogram_log.h>
#include <hdr_interval_recorder.h>
#include <hdr_time.h>

static int64_t diff(struct timespec t0, struct timespec t1)
{
    int64_t delta_us = 0;
    delta_us = (t1.tv_sec - t0.tv_sec) * 1000000;
    delta_us += (t1.tv_nsec - t0.tv_nsec) / 1000;

    return delta_us;
}

static void update_histogram(void* data, void* arg)
{
    struct hdr_histogram* h = data;
    int64_t* values = arg;

    hdr_record_value(h, values[0]);
}

static void* record_hiccups(void* thread_context)
{
    struct pollfd fd;
    struct timespec t0;
    struct timespec t1;
    struct itimerspec timeout; 
    struct hdr_interval_recorder* r = thread_context;

    memset(&fd, 0, sizeof(struct pollfd));
    memset(&timeout, 0, sizeof(struct itimerspec));
    memset(&t0, 0, sizeof(struct timespec));
    memset(&t1, 0, sizeof(struct timespec));

    fd.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
    fd.events = POLLIN|POLLPRI|POLLRDHUP;
    fd.revents = 0;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
    while (true)
    {
        timeout.it_value.tv_sec = 0;
          timeout.it_value.tv_nsec = 1000000;
        timerfd_settime(fd.fd, 0, &timeout, NULL);

        hdr_gettime(&t0);
        poll(&fd, 1, -1);
        hdr_gettime(&t1);

        int64_t delta_us = diff(t0, t1) - 1000;
        delta_us = delta_us < 0 ? 0 : delta_us;

        hdr_interval_recorder_update(r, update_histogram, &delta_us);
    }
#pragma clang diagnostic pop

    pthread_exit(NULL);
}

struct config_t
{
    int interval;
    const char* filename;
};

const char* USAGE =
"hiccup [-i <interval>] [-f <filename>]\n"
"  interval: <number> Time in seconds between samples (default 1).\n"
"  filename: <string> Name of the file to log to (default stdout).\n";

static int handle_opts(int argc, char** argv, struct config_t* config)
{
    int c;
    int interval = 1;

    while ((c = getopt(argc, argv, "i:f:")) != -1)
    {
        switch (c)
        {
        case 'h':
            return 0;

        case 'i':
            interval = atoi(optarg);
            if (interval < 1)
            {
                return 0;
            }

            break;
        case 'f':
            config->filename = optarg;
            break;
        default:
            return 0;
        }
    }

    config->interval = interval < 1 ? 1 : interval;
    return 1;
}

int main(int argc, char** argv)
{
    struct timespec timestamp;
    struct timespec start_timestamp;
    struct timespec end_timestamp;
    struct hdr_interval_recorder recorder;
    struct hdr_log_writer log_writer;
    struct config_t config;
    pthread_t recording_thread;
    FILE* output = stdout;

    memset(&config, 0, sizeof(struct config_t));
    if (!handle_opts(argc, argv, &config))
    {
        printf("%s", USAGE);
        return 0;
    }

    if (config.filename)
    {
        output = fopen(config.filename, "a+");
        if (!output)
        {
            fprintf(
                stderr, "Failed to open/create file: %s, %s", 
                config.filename, strerror(errno));

            return -1;
        }
    }

    if (0 != hdr_interval_recorder_init(&recorder))
    {
        fprintf(stderr, "%s\n", "Failed to init phaser");
        return -1;
    }

    if (0 != hdr_init(
        1, INT64_C(24) * 60 * 60 * 1000000, 3,
        (struct hdr_histogram**) &recorder.active))
    {
        fprintf(stderr, "%s\n", "Failed to init hdr_histogram");
        return -1;
    }

    if (0 != hdr_init(
        1, INT64_C(24) * 60 * 60 * 1000000, 3,
        (struct hdr_histogram**) &recorder.inactive))
    {
        fprintf(stderr, "%s\n", "Failed to init hdr_histogram");
        return -1;
    }

    if (pthread_create(&recording_thread, NULL, record_hiccups, &recorder))
    {
        fprintf(stderr, "%s\n", "Failed to create thread");
        return -1;
    }

    hdr_gettime(&start_timestamp);
    hdr_log_writer_init(&log_writer);
    hdr_log_write_header(&log_writer, output, "foobar", &timestamp);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
    while (true)
    {        
        sleep(config.interval);

        hdr_reset(recorder.inactive);
        struct hdr_histogram* h = hdr_interval_recorder_sample(&recorder);

        hdr_gettime(&end_timestamp);
        timestamp = start_timestamp;

        hdr_gettime(&start_timestamp);

        hdr_log_write(&log_writer, output, &timestamp, &end_timestamp, h);
        fflush(output);
    }
#pragma clang diagnostic pop

    pthread_exit(NULL);
}