1/*
2 * Copyright (c) 2002-2004, 2008 Apple Computer, Inc.  All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#include <stdlib.h>
24#include <sys/time.h>
25#include "libtop.h"
26#include "cpu.h"
27#include "generic.h"
28#include "preferences.h"
29#include "log.h"
30
31extern const libtop_tsamp_t *tsamp;
32
33static bool cpu_insert_cell(struct statistic *s, const void *sample) {
34    const libtop_psamp_t *psamp = sample;
35    struct timeval elapsed, used;
36    char buf[10];
37    unsigned long long elapsed_us = 0, used_us = 0;
38    int whole = 0, part = 0;
39
40    if(0 == psamp->p_seq) {
41	whole = 0;
42	part = 0;
43
44	if(-1 == snprintf(buf, sizeof(buf), "%d.%1d", whole, part))
45	    return true;
46
47	return generic_insert_cell(s, buf);
48    }
49
50
51    switch(top_prefs_get_mode()) {
52    case STATMODE_ACCUM:
53	timersub(&tsamp->time, &tsamp->b_time, &elapsed);
54	timersub(&psamp->total_time, &psamp->b_total_time, &used);
55	break;
56
57
58    case STATMODE_EVENT:
59    case STATMODE_DELTA:
60    case STATMODE_NON_EVENT:
61	timersub(&tsamp->time, &tsamp->p_time, &elapsed);
62	timersub(&psamp->total_time, &psamp->p_total_time, &used);
63	break;
64
65    default:
66	fprintf(stderr, "unhandled STATMOMDE in %s\n", __func__);
67	abort();
68    }
69
70    elapsed_us = (unsigned long long)elapsed.tv_sec * 1000000ULL
71	+ (unsigned long long)elapsed.tv_usec;
72
73    used_us = (unsigned long long)used.tv_sec * 1000000ULL
74	+ (unsigned long long)used.tv_usec;
75
76    /* Avoid a divide by 0 exception. */
77    if(elapsed_us > 0) {
78	whole = (used_us * 100ULL) / elapsed_us;
79	part = (((used_us * 100ULL) - (whole * elapsed_us)) * 10ULL) / elapsed_us;
80    }
81
82    //top_log("command %s whole %d part %d\n", psamp->command, whole, part);
83
84    if(-1 == snprintf(buf, sizeof(buf), "%d.%1d", whole, part))
85	return true;
86
87    return generic_insert_cell(s, buf);
88}
89
90static struct statistic_callbacks callbacks = {
91    .draw = generic_draw,
92    .resize_cells = generic_resize_cells,
93    .move_cells = generic_move_cells,
94    .get_request_size = generic_get_request_size,
95    .get_minimum_size = generic_get_minimum_size,
96    .insert_cell = cpu_insert_cell,
97    .reset_insertion = generic_reset_insertion
98};
99
100struct statistic *top_cpu_create(WINDOW *parent, const char *name) {
101    return create_statistic(STATISTIC_CPU, parent, NULL, &callbacks, name);
102}
103