File: hybridCpu.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (313 lines) | stat: -rwxr-xr-x 9,634 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* $Id: hybridCpu.c,v 1.32 2004/09/27 21:59:04 graziano Exp $ */

/* #define VERBOSE */

#include "config_nws.h"

#include <math.h> 
#include <string.h>
#include <stdlib.h>

#include "diagnostic.h"  /* FAIL() LOG() */
#include "osutil.h"      /* CPUCount() */
#include "strutil.h"
#include "activeCpu.h"
#include "passiveCpu.h"
#include "hybridCpu.h"
#include "skills.h"


#define MAX_FUSE 1000
#define MAX_NICE_VALUES 20
#define MAX_RESOURCES 10
#define TAME_VARIATION 0.01
#define WILD_VARIATION 0.15


/*
** Module variables.  These six are set by HybridCpuOpenMonitor() for reference
** by HybridCpuGetLoad().  #adaptingLength# and #adaptingPeriod# indicate
** whether or not we're using heuristics to adapt how long or how frquently we
** run the active sensor.  #activeProbeLength# and #activeProbePeriod# are the
** initial values for how long (in msecs) and how frequently (in terms of
** number of passive probes) we run active probes.  #passiveResources# caches
** the value returned by PassiveCpuResourceCount(); #totalCpus# the number of
** cpus on the system.
*/
static int adaptingLength;
static int adaptingPeriod;
static unsigned int activeProbeLength;
static int activeProbePeriod = PASSIVE_ONLY;
static int passiveResources;
static int totalCpus;

/*
** For each nice value that we're tracking, we initialize a TrackInfo struct in
** HybridCpuOpenMonitor(), then use heuristics to update it in
** HybridCpuGetLoad().  #fuse# is reduced by some fraction of #burn# every time
** HybridCpuGetLoad() runs a passive test until it reaches zero, at which point
** the function runs an active test.  #passiveError# holds the difference
** between the value reported by the prior active probe for each resource and
** the passive probe run at the same time.  #passivePrior# is the value
** reported by the previous passive probe for the #bestResource# test.
** #passiveSinceActive# is the number of passive probes which have been run
** since the most recent active probe.
*/
struct TrackInfo {
  unsigned short nice;
  int bestResource;
  int burn;
  int fuse;
  double passiveError[MAX_RESOURCES];
  double passivePrior;
  int passiveSinceActive;
};
static struct TrackInfo trackers[MAX_NICE_VALUES];
static int trackCount = 0;

void
HybridCpuUseSkill(const char * options, int *length, SkillResult **results) {
	static char *cpuOptions = NULL;
	char niceValue[7 + 1], *tmp;
	int niceValuesCount;
	unsigned short niceValues[20];
	const char *c;
	char opts[255 + 1];
	double res[2];

	if((cpuOptions == NULL) || (strcmp(cpuOptions, options) != 0)) {
		/* (Re)initialize the CPU monitor using the nice value(s)
		 * in #options#. */
		if(cpuOptions != NULL) {
			HybridCpuCloseMonitor();
			free(cpuOptions);
		}
		cpuOptions = strdup(options);
		niceValuesCount = 0;
		tmp = GetOptionValue(options, "nice", "0");
		for (c = tmp; GETTOK(niceValue, c, ",", &c);) {
			niceValues[niceValuesCount++] = strtol(niceValue, NULL, 10);
		}
		FREE(tmp);
		HybridCpuOpenMonitor(niceValues,
					niceValuesCount,
					10, /* see NOTE above */
					INITIAL_ACTIVE_FREQ,
					ADAPT,
					DEFAULT_ACTIVE_LENGTH,
					DONT_ADAPT);
	}
	/* Get measurements for each nice value. */
	tmp = GetOptionValue(options, "nice", "0");
	for(c = tmp; GETTOK(niceValue, c, ",", &c);) {
		vstrncpy(opts, sizeof(opts), 2, "nice:", niceValue);
		if(HybridCpuGetLoad(strtol(niceValue, NULL, 10), &res[0], &res[1])) {
			AppendResult(availableCpu, opts, 1, res[0], length, results);
			AppendResult(currentCpu, opts, 1, res[1], length, results);
			DDEBUG2("HybridCpuUseSkill: got %.4f availableCPu and %.4f currentCpu\n", res[0], res[1]);
		} else {
			AppendResult(availableCpu, opts, 0, 0.0, length, results);
			AppendResult(currentCpu, opts, 0, 0.0, length, results);
		}
	}
	FREE(tmp);
}


int
HybridCpuCloseMonitor(void) {
	return(PassiveCpuCloseMonitor());
}


int
HybridCpuGetLoad(unsigned short niceValue,
                 double *newFraction,
                 double *existingFraction) {

  double activeAvail;
  int activeResult;
  double bestError;
  int i;
  double passiveAvail[MAX_RESOURCES];
  double passiveReport;
  int passiveResult[MAX_RESOURCES];
  struct TrackInfo *track;
  static int testNumber = 0;

  for(i = 0; i < trackCount; i++) {
    if(trackers[i].nice == niceValue)
      break;
  }

  if(i >= trackCount) {
    FAIL1("hybridCpuGetLoad: untracked nice value %d\n", niceValue);
  }

  track = &trackers[i];
  track->passiveSinceActive++;
  testNumber++;

  for(i = 0; i < passiveResources; i++) {
    passiveResult[i] =
      PassiveCpuGetLoad(i, niceValue, &passiveAvail[i], existingFraction);
  }

  if(!passiveResult[track->bestResource]) {
    FAIL("hybridCpuGetLoad: passive resource failed\n");
  }

  *newFraction = passiveAvail[track->bestResource] +
                 track->passiveError[track->bestResource];
  /* Check for obviously-incorrect values. */
  if(*newFraction < 0.0)
  {
    *newFraction = 0.0;
  }
  else if(*newFraction > totalCpus)
  {
    *newFraction = totalCpus;
    track->fuse = 0;			/* assume that the problem is
					   that we need to recalibrate =>
					   force an active probe */
  }

  /*
  ** Heuristic description: if the values reported by the passive probe begin
  ** to vary wildly, then the error we stored on the most recent active probe
  ** is stale and we want to move more quickly toward a new active probe.
  ** When we run an active probe, if the passive error is fairly consistent
  ** then we can relax a bit; otherwise, we want to move toward running
  ** active probes more frequently.
  */

  if(activeProbePeriod != PASSIVE_ONLY) {

    if(!adaptingPeriod)
      track->fuse -= track->burn;
    else if(fabs(track->passivePrior - passiveAvail[track->bestResource]) >=
            WILD_VARIATION)
      track->fuse -= track->burn * 2;
    else if(fabs(track->passivePrior - passiveAvail[track->bestResource]) <=
            TAME_VARIATION)
      track->fuse -= track->burn / 2;
    else
      track->fuse -= track->burn;

    if(track->fuse <= 0) {

      /* Run an active probe and reset all counters. */
      activeResult =
        ActiveCpuGetLoad(niceValue, 60, activeProbeLength, &activeAvail);
      LOG2("hybridCpu: Active probe %d reports %f\n", testNumber, activeAvail);

      if(activeResult) {

        bestError = 101.0;
        for(i = 0; i < passiveResources; i++) {
          if( passiveResult[i] &&
              (fabs(activeAvail - passiveAvail[i]) < fabs(bestError)) ) {
            bestError = activeAvail - passiveAvail[i];
            track->bestResource = i;
          }
        }
        LOG1("hybridCpu: using %s for passive probes.\n",
             PassiveCpuResourceName(track->bestResource));

        if(!adaptingPeriod || (track->passiveSinceActive <= 1))
          track->burn = MAX_FUSE / activeProbePeriod;
        else if(fabs(bestError - track->passiveError[track->bestResource]) >=
                WILD_VARIATION)
          track->burn = MAX_FUSE / (track->passiveSinceActive / 2);
        else if(fabs(bestError - track->passiveError[track->bestResource]) <=
                TAME_VARIATION)
          track->burn = MAX_FUSE / (track->passiveSinceActive * 2);
        else
          track->burn = MAX_FUSE / (track->passiveSinceActive + 1);
        *newFraction = activeAvail;
        for(i = 0; i < passiveResources; i++) {
          track->passiveError[i] = activeAvail - passiveAvail[i];
        }

      }

      track->fuse = MAX_FUSE;
      track->passiveSinceActive = 0;

    }

  }

#ifdef VERBOSE
  for(i = 0; i < passiveResources; i++) {
    passiveReport = passiveAvail[i] + passiveError[i];
    /* Check for obviously-incorrect values. */
    if(passiveReport < 0.0)
      passiveReport = 0.0;
    else if(passiveReport > totalCpus)
      passiveReport = totalCpus;
    LOG3("hybridCpu: %s probe %d reports %f\n",
         PassiveCpuResourceName(i), testNumber, passiveAvail[i]);
    LOG3("hybridCpu: %s probe %d returns %f\n",
         PassiveCpuResourceName(i), testNumber, passiveReport);
  }
#else
  passiveReport = 0.0; /* Avoid compiler warning. */
#endif

  track->passivePrior = passiveAvail[track->bestResource];
  return(1);

}


int
HybridCpuMonitorAvailable(const char *options) {
  return PassiveCpuMonitorAvailable();
}


int
HybridCpuOpenMonitor(const unsigned short *niceValues,
                     int niceCount,
                     int checkFrequency,
                     int activePeriod,
                     int adaptPeriod,
                     int activeLength,
                     int adaptLength) {

  int i;
  int resource;

  LOG2("HybridCPUOpenMonitor: run every %d secs, active every %dth time\n",
       checkFrequency, activePeriod);

  totalCpus = CPUCount();
  passiveResources = PassiveCpuResourceCount();
  trackCount = niceCount;
  /* if we have more than 1 cpu, current active probes may not be accurate */
  /* this will cause activeProbePeriod to remain PASSIVE_ONLY */
  if (totalCpus == 1) {
        activeProbeLength = activeLength;
        activeProbePeriod = activePeriod;
        adaptingLength = adaptLength;
        adaptingPeriod = adaptPeriod;
  }

  LOG1("HybridCPUOpenMonitor: %d CPUs detected\n", totalCpus);

  for(i = 0; i < trackCount; i++) {
    trackers[i].nice = niceValues[i];
    trackers[i].bestResource = 0;
    trackers[i].burn = 0;
    trackers[i].fuse = 0; /* This forces an initial active probe. */
    trackers[i].passivePrior = 0.0;
    trackers[i].passiveSinceActive = 0;
    for (resource = 0; resource < passiveResources; resource++) {
      trackers[i].passiveError[resource] = 0.0;
    }
  }

  return(PassiveCpuOpenMonitor(checkFrequency));

}