Skip to content

Commit

Permalink
Update clock() syscall to keep track of effective cpu time
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Dec 15, 2018
1 parent 07764cd commit bdd9b2a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
6 changes: 6 additions & 0 deletions kernel/src/proc/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ int do_signal(void)
return 0;
}


void scheduler(void)
{
struct task *curr;
struct task *next;
static clock_t prev_clock;

curr = current;
next = list_container(current->tasks.next,
Expand All @@ -102,6 +104,10 @@ void scheduler(void)
next = &ktask;
}

/* Update CPU usage statistics */
current->usage += (timer_ticks - prev_clock);
prev_clock = timer_ticks;

current = next;
current->counter = msecs_to_ticks(SCHED_TIMESLICE);

Expand Down
1 change: 1 addition & 0 deletions kernel/src/proc/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int task_init(struct task *tsk, task_entry_t entry)
tsk->brk = current->brk;

/* sheduler */
tsk->usage = 0;
tsk->state = TASK_RUNNING;
tsk->counter = msecs_to_ticks(SCHED_TIMESLICE);
tsk->exit_code = 0;
Expand Down
1 change: 1 addition & 0 deletions kernel/src/proc/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct task {
struct timer_event alarm; /**< Alarm timer event (pre-allocated) */
struct list_link condw; /**< Conditional wait */
dev_t tty; /**< Controlling terminal */
clock_t usage; /**< CPU time in clock ticks */
};


Expand Down
4 changes: 2 additions & 2 deletions kernel/src/sys/sys_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/

#include "sys.h"
#include "timer.h"
#include "proc.h"

unsigned int sys_clock(void)
{
return (unsigned int)timer_ticks;
return (unsigned int)current->usage;
}
2 changes: 1 addition & 1 deletion kernel/src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "timer.h"
#include "proc.h"

unsigned long timer_ticks = 0;
clock_t timer_ticks = 0;

/* Timer events queue. */
static struct list_link timer_events;
Expand Down
5 changes: 2 additions & 3 deletions kernel/src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@


/* Clock ticks since system startup. */
extern unsigned long timer_ticks;
extern clock_t timer_ticks;

/** Converts milliseconds to clock ticks. */
#define msecs_to_ticks(msecs) \
(((unsigned long)(msecs) + \
(1000L / CLOCKS_PER_SEC) - 1) / (1000L / CLOCKS_PER_SEC))
(((unsigned long)(msecs) + (1000L / CLOCKS_PER_SEC) - 1) / (1000L / CLOCKS_PER_SEC))

#define ticks_to_msecs(ticks) \
((1000L / CLOCKS_PER_SEC) * (unsigned long)(ticks))
Expand Down
5 changes: 0 additions & 5 deletions libc/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ struct timespec {
long tv_nsec; /**> Nanoseconds */
};

/*
* The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is
* the number per second of the value returned by the `clock' function.
* The value of CLOCKS_PER_SEC is required to be 1 million on all XSI systems.
*/
#define CLOCKS_PER_SEC ((clock_t) 100)

clock_t clock(void);
Expand Down
13 changes: 11 additions & 2 deletions user/src/test/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@

int main(void)
{
clock_t prev;

prev = clock();
while (1) {
printf("Hello from %d (%u)\n", getpid(), clock());
sleep(1);
if ((clock() - prev)/CLOCKS_PER_SEC >= 3) {
printf("\n");
printf("Going to sleep...(cpu time: ~%u)\n",
clock()/ CLOCKS_PER_SEC);
prev = clock();
sleep(3);
}
printf(".");
}
return 0;
}

0 comments on commit bdd9b2a

Please sign in to comment.