1/*
2 * Copyright (c) 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 <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <inttypes.h>
27#include "libtop.h"
28#include "workqueue.h"
29#include "generic.h"
30#include "uinteger.h"
31
32static bool workqueue_insert_cell(struct statistic *s, const void *sample) {
33    const libtop_psamp_t *psamp = sample;
34    char runbuf[GENERIC_INT_SIZE];
35    char totalbuf[GENERIC_INT_SIZE];
36    char buf[GENERIC_INT_SIZE * 2 + 1];
37    struct top_uinteger run, total;
38
39    run = top_uinteger_calc_result(psamp->wq_run_threads,
40				   psamp->p_wq_run_threads, 0ULL);
41
42    total = top_uinteger_calc_result(psamp->wq_nthreads,
43				     psamp->p_wq_nthreads, 0ULL);
44
45    if(top_sprint_uinteger(runbuf, sizeof(runbuf), run))
46		return true;
47
48    if(top_sprint_uinteger(totalbuf, sizeof(totalbuf), total))
49        return true;
50
51    if(0 != run.value) {
52        if(-1 == snprintf(buf, sizeof(buf), "%s/%s", totalbuf, runbuf))
53            return true;
54    } else {
55        if(-1 == snprintf(buf, sizeof(buf), "%s", totalbuf))
56            return true;
57    }
58
59    return generic_insert_cell(s, buf);
60}
61
62static struct statistic_callbacks callbacks = {
63    .draw = generic_draw,
64    .resize_cells = generic_resize_cells,
65    .move_cells = generic_move_cells,
66    .get_request_size = generic_get_request_size,
67    .get_minimum_size = generic_get_minimum_size,
68    .insert_cell = workqueue_insert_cell,
69    .reset_insertion = generic_reset_insertion
70};
71
72struct statistic *top_workqueue_create(WINDOW *parent, const char *name) {
73    return create_statistic(STATISTIC_WORKQUEUE, parent, NULL, &callbacks, name);
74}
75