File: profiler.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (149 lines) | stat: -rw-r--r-- 4,442 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
/*=============================================================================

    This file is part of FLINT.

    FLINT 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 2 of the License, or
    (at your option) any later version.

    FLINT 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 FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2007 William Hart and David Harvey
    Copyright (C) 2013 Fredrik Johansson

******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include "profiler.h"

/*
   clock_last[i] is the last read clock value for clock #i.
   clock_accum[i] is the total time attributed to clock #i so far.
   These should not be read directly; use get_clock(i) instead.
 */
double clock_last[FLINT_NUM_CLOCKS];
double clock_accum[FLINT_NUM_CLOCKS];

void
prof_repeat(double *min, double *max, profile_target_t target, void *arg)
{
    /* Number of timings that were at least DURATION_THRESHOLD microseconds */
    ulong good_count = 0;
    double max_time = DBL_MIN, min_time = DBL_MAX;

    /* First try one loop */
    ulong num_trials = 4;
    double last_time;
    init_clock(0);
    target(arg, num_trials);
    last_time = get_clock(0);

    /* Loop until we have enough good times */
    while (1)
    {
        double per_trial = last_time / num_trials;

        /* If the last recorded time was long enough, record it */
        if (last_time > DURATION_THRESHOLD)
        {
            if (good_count)
            {
                if (per_trial > max_time)
                    max_time = per_trial;
                if (per_trial < min_time)
                    min_time = per_trial;
            }
            else
                max_time = min_time = per_trial;

            if (++good_count == 5)
            {
                /* We've got enough data */
                break;
            }
        }

        /*
           Adjust num_trials so that the elapsed time gravitates towards
           DURATION_TARGET; num_trials can be changed by a factor of
           at most 25%, and must be at least 1
         */
        {
            double adjust_ratio;
            if (last_time < 0.0001)
                last_time = 0.0001;
            adjust_ratio = DURATION_TARGET / last_time;
            if (adjust_ratio > 1.25)
                adjust_ratio = 1.25;
            if (adjust_ratio < 0.75)
                adjust_ratio = 0.75;
            num_trials = (ulong) ceil(adjust_ratio * num_trials);
            /* Just to be safe */
            if (num_trials == 0)
                num_trials = 1;
        }

        /* Run another trial */
        init_clock(0);
        target(arg, num_trials);
        last_time = get_clock(0);
    }

    /* Store results */
    if (min)
        *min = min_time;
    if (max)
        *max = max_time;
}

void get_memory_usage(meminfo_t meminfo)
{
    FILE * file = fopen("/proc/self/status", "r");

    ulong result;
    char line[128];

    while (fgets(line, 128, file) != NULL)
    {
        result = 0;

        if (strncmp(line, "VmSize:", 7) == 0)
        {
            flint_sscanf(line, "VmSize: %wu kB\n", &result);
            meminfo->size = result;
        }
        else if (strncmp(line, "VmPeak:", 7) == 0)
        {
            flint_sscanf(line, "VmPeak: %wu kB\n", &result);
            meminfo->peak = result;
        }
        else if (strncmp(line, "VmHWM:", 6) == 0)
        {
            flint_sscanf(line, "VmHWM: %wu kB\n", &result);
            meminfo->hwm = result;
        }
        else if (strncmp(line, "VmRSS:", 6) == 0)
        {
            flint_sscanf(line, "VmRSS: %wu kB\n", &result);
            meminfo->rss = result;
        }
    }

    fclose(file);
}