aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc/rusage.c
blob: 972ae4f4cc6ae194116f05efcae7b48b4a711680 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */

#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */

#include "../common/utils.h"
#include "rusage.h"

// FIXME: optimize to use a single syscall for both cpu time and memory?


long long
blogc_rusage_get_cpu_time(void)
{
#ifndef HAVE_RUSAGE
    return 0;
#else
    struct rusage usage;
    if (0 != getrusage(RUSAGE_SELF, &usage))
        return 0;
    return (
        (usage.ru_utime.tv_sec * 1000000) + usage.ru_utime.tv_usec +
        (usage.ru_stime.tv_sec * 1000000) + usage.ru_stime.tv_usec);
#endif
}


char*
blogc_rusage_format_cpu_time(long long time)
{
    if (time > 1000000)
        return bc_strdup_printf("%.3s", ((float) time) / 1000000.0);

    // this is a special case: some systems may report the cpu time rounded up to the
    // milisecond. it is useless to show ".000" in this case.
    if (time > 1000)
        return bc_strdup_printf("%.*fms", time % 1000 ? 3 : 0, ((float) time) / 1000.0);

    return bc_strdup_printf("%dus", time);
}


char*
blogc_rusage_cpu_time(void)
{
    return blogc_rusage_format_cpu_time(blogc_rusage_get_cpu_time());
}


long
blogc_rusage_get_memory(void)
{
#ifndef HAVE_RUSAGE
    return 0;
#else
    struct rusage usage;
    if (0 != getrusage(RUSAGE_SELF, &usage))
        return 0;
    return usage.ru_maxrss;
#endif
}


char*
blogc_rusage_format_memory(long mem)
{
    if (mem > 1048576)
        return bc_strdup_printf("%.3fGB", ((float) mem) / 1048576.0);
    if (mem > 1024)
        return bc_strdup_printf("%.3fMB", ((float) mem) / 1024.0);
    return bc_strdup_printf("%dKB", mem);
}


char*
blogc_rusage_memory(void)
{
    return blogc_rusage_format_memory(blogc_rusage_get_memory());
}