source: mainline/kernel/generic/src/ps/load.c@ c0379fc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0379fc was e257ae3, checked in by Stanislav Kozina <stanislav.kozina@…>, 16 years ago

Counting CPU busy and idle clock ticks.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericload
30 * @{
31 */
32
33/**
34 * @file
35 * @brief System load computation.
36 */
37
38#include <proc/thread.h>
39#include <ps/load.h>
40#include <arch.h>
41#include <proc/scheduler.h>
42#include <config.h>
43#include <arch/types.h>
44#include <time/clock.h>
45#include <syscall/copy.h>
46
47static size_t get_running_count(void);
48
49size_t avenrun[3];
50
51#define FSHIFT 11 /* nr of bits of precision */
52#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
53#define LOAD_FREQ 5 /* 5 sec intervals */
54#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
55#define EXP_5 2014 /* 1/exp(5sec/5min) */
56#define EXP_15 2037 /* 1/exp(5sec/15min) */
57
58#define CALC_LOAD(load,exp,n) \
59 load *= exp; \
60 load += n*(FIXED_1-exp); \
61 load >>= FSHIFT;
62
63static inline unsigned long calc_load(size_t load, size_t exp, size_t active)
64{
65 load *= exp;
66 load += active * (FIXED_1 - exp);
67 return load >> FSHIFT;
68}
69
70static inline void calc_load_global(void)
71{
72 size_t active;
73
74 active = get_running_count();
75 active = active > 0 ? active * FIXED_1 : 0;
76 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
77 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
78 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
79}
80
81static size_t get_running_count(void)
82{
83 size_t i;
84 size_t result = 0;
85 ipl_t ipl;
86
87 /* run queues should not change during reading */
88 ipl = interrupts_disable();
89
90 for (i = 0; i < config.cpu_active; ++i) {
91 cpu_t *cpu = &cpus[i];
92 int j;
93 for (j = 0; j < RQ_COUNT; ++j) {
94 result += cpu->rq[j].n;
95 }
96 }
97
98 interrupts_restore(ipl);
99 return result;
100}
101
102/** Load thread main function.
103 * Thread computes system load every few seconds.
104 *
105 * @param arg Generic thread argument (unused).
106 *
107 */
108void kload_thread(void *arg)
109{
110 /* Noone will thread_join us */
111 thread_detach(THREAD);
112 avenrun[0] = 0;
113 avenrun[1] = 0;
114 avenrun[2] = 0;
115
116 while (true) {
117 calc_load_global();
118 thread_sleep(LOAD_FREQ);
119 }
120}
121
122int sys_ps_get_load(size_t *user_load)
123{
124 copy_to_uspace(user_load, avenrun, sizeof(avenrun));
125 return 0;
126}
127
128
129/** @}
130 */
Note: See TracBrowser for help on using the repository browser.