Changeset 944f2cab in mainline for kernel/generic/src/ps/load.c


Ignore:
Timestamp:
2010-03-31T09:30:04Z (14 years ago)
Author:
Stanislav Kozina <stanislav.kozina@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e257ae3
Parents:
3a10e34
Message:

ps -l echoes system load

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/ps/load.c

    r3a10e34 r944f2cab  
    4040#include <ps/load.h>
    4141#include <arch.h>
     42#include <proc/scheduler.h>
     43#include <config.h>
     44#include <arch/types.h>
     45#include <time/clock.h>
     46#include <syscall/copy.h>
     47
     48static size_t get_running_count(void);
     49
     50size_t avenrun[3];
     51
     52#define FSHIFT   11             /* nr of bits of precision */
     53#define FIXED_1  (1<<FSHIFT)    /* 1.0 as fixed-point */
     54#define LOAD_FREQ 5             /* 5 sec intervals */
     55#define EXP_1  1884             /* 1/exp(5sec/1min) as fixed-point */
     56#define EXP_5  2014             /* 1/exp(5sec/5min) */
     57#define EXP_15 2037             /* 1/exp(5sec/15min) */
     58
     59#define CALC_LOAD(load,exp,n) \
     60        load *= exp; \
     61        load += n*(FIXED_1-exp); \
     62        load >>= FSHIFT;
     63
     64static inline unsigned long calc_load(size_t load, size_t exp, size_t active)
     65{
     66        load *= exp;
     67        load += active * (FIXED_1 - exp);
     68        return load >> FSHIFT;
     69}
     70
     71static inline void calc_load_global(void)
     72{
     73        size_t active;
     74
     75        active = get_running_count();
     76        active = active > 0 ? active * FIXED_1 : 0;
     77        avenrun[0] = calc_load(avenrun[0], EXP_1, active);
     78        avenrun[1] = calc_load(avenrun[1], EXP_5, active);
     79        avenrun[2] = calc_load(avenrun[2], EXP_15, active);
     80}
     81
     82static size_t get_running_count(void)
     83{
     84        size_t i;
     85        size_t result = 0;
     86        ipl_t ipl;
     87
     88        /* run queues should not change during reading */
     89        ipl = interrupts_disable();
     90
     91        for (i = 0; i < config.cpu_active; ++i) {
     92                cpu_t *cpu = &cpus[i];
     93                int j;
     94                for (j = 0; j < RQ_COUNT; ++j) {
     95                        result += cpu->rq[j].n;
     96                }
     97        }
     98
     99        interrupts_restore(ipl);
     100        return result;
     101}
    42102
    43103/** Load thread main function.
     
    51111        /* Noone will thread_join us */
    52112        thread_detach(THREAD);
     113        avenrun[0] = 0;
     114        avenrun[1] = 0;
     115        avenrun[2] = 0;
    53116
    54117        while (true) {
    55                 printf("load thread alive\n");
    56                 thread_sleep(5);
     118                calc_load_global();
     119                printf("Computed loads: 0x%x 0x%x 0x%x\n", avenrun[0], avenrun[1], avenrun[2]);
     120                thread_sleep(LOAD_FREQ);
    57121        }
     122}
     123
     124int sys_ps_get_load(size_t *user_load)
     125{
     126        copy_to_uspace(user_load, avenrun, sizeof(avenrun));
     127        return 0;
    58128}
    59129
Note: See TracChangeset for help on using the changeset viewer.