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#ifndef STATISTIC_H
24#define STATISTIC_H
25
26#include <stdio.h>
27#include <inttypes.h>
28#include <curses.h>
29#include <panel.h>
30#include <stdbool.h>
31
32/*
33 * Extend this if we add another type.
34 * These types are used for sorting, and priority of display.
35 */
36enum {
37    /*Begin same as old top*/
38    STATISTIC_PID, STATISTIC_COMMAND, STATISTIC_CPU, STATISTIC_TIME,
39    STATISTIC_THREADS, STATISTIC_PORTS, STATISTIC_MREGION,
40#ifdef TOP_ANONYMOUS_MEMORY
41    STATISTIC_RMEM, STATISTIC_RPRVT, STATISTIC_PURG, STATISTIC_COMPRESSED,
42#else
43    STATISTIC_RPRVT, STATISTIC_RSHRD, STATISTIC_RSIZE,
44#endif
45    STATISTIC_VSIZE,
46    /*End same as old top*/
47    STATISTIC_VPRVT,
48    STATISTIC_PGRP, STATISTIC_PPID, STATISTIC_PSTATE,
49    STATISTIC_UID, STATISTIC_WORKQUEUE, STATISTIC_FAULTS, STATISTIC_COW_FAULTS,
50    STATISTIC_MESSAGES_SENT, STATISTIC_MESSAGES_RECEIVED, STATISTIC_SYSBSD,
51    STATISTIC_SYSMACH, STATISTIC_CSW, STATISTIC_PAGEINS, STATISTIC_KPRVT, STATISTIC_KSHRD,
52    STATISTIC_IDLEWAKE,
53    STATISTIC_POWERSCORE,
54    STATISTIC_USER
55};
56
57enum {
58    STATISTIC_TOTAL = STATISTIC_USER + 1
59};
60
61struct statistic;
62struct statistics_controller;
63
64struct statistic_name_map {
65    int e;
66    struct statistic *(*creator)(WINDOW *parent, const char *name);
67    const char *name;
68};
69
70extern struct statistic_name_map statistic_name_map[];
71
72struct statistic_state {
73    int type;
74    char name[50];
75    struct statistic *(*create_statistic)(WINDOW *parent, const char *name);
76    struct statistic *instance;
77};
78
79struct statistics_controller {
80    void *globalstats;
81    WINDOW *parent;
82    struct statistic_state state[STATISTIC_TOTAL];
83    int total_possible_statistics;
84    int total_active_statistics;
85
86    void (*reset_insertion)(struct statistics_controller *c);
87    void (*insert_sample)(struct statistics_controller *c, const void *sample);
88    void (*remove_tail)(struct statistics_controller *c);
89    void (*insert_tail)(struct statistics_controller *c);
90    int (*get_total_possible)(struct statistics_controller *c);
91    int (*get_total_active)(struct statistics_controller *c);
92    void (*iterate)(struct statistics_controller *c,
93		    bool (*func)(struct statistic *, void *),
94		    void *ptr);
95};
96
97struct statistic_size {
98    int width, height;
99};
100
101struct statistic_destructor {
102    /* This provides a destructor that may be called to cleanup a statistic. */
103    void (*destructor) (struct statistic *s, void *ptr);
104    /* This is passed to the callback stored above. */
105    void *ptr;
106    struct statistic_destructor *next;
107};
108
109struct statistic_callbacks {
110    void (*draw) (struct statistic *s, int x);
111    bool (*resize_cells) (struct statistic *s, struct statistic_size *size);
112    bool (*move_cells) (struct statistic *s, int x, int y);
113    void (*get_request_size) (struct statistic *s);
114    void (*get_minimum_size) (struct statistic *s);
115    bool (*insert_cell) (struct statistic *s, const void *sample);
116    void (*reset_insertion) (struct statistic *s);
117};
118
119struct statistic {
120    WINDOW *parent;
121    WINDOW *window;
122    PANEL *panel;
123    int type; /* The STATISTIC enum type from above. */
124    void *cells; /* This usually stores a struct generic_cell. */
125    void *ptr; /* This is for private data created at the time of
126		* create_statistic.
127		*/
128    char *header;
129
130    bool visible;
131
132    struct statistics_controller *controller;
133
134    /* Used for profiling: */
135    uint64_t time_consumed;
136    uint64_t runs;
137
138    struct statistic_callbacks callbacks;
139    struct statistic_destructor *destructors;
140    struct statistic_size request_size; /* Used with get_request_size */
141    struct statistic_size actual_size;
142    struct statistic_size minimum_size;
143    struct statistic *previous, *next;
144};
145
146
147/* This should be called before any of the functions below: */
148struct statistics_controller *create_statistics_controller(WINDOW *parent);
149
150/*
151 * This takes a STATISTIC_ enum type from above, a WINDOW parent for a derwin,
152 * and a pointer of some sort (if desired for extra data).
153 *
154 * This returns NULL when an error occurs.
155 */
156struct statistic *create_statistic(int type, WINDOW *parent, void *ptr,
157				   struct statistic_callbacks *callbacks,
158				   const char *name);
159
160/*
161 * This destroys the statistic and calls any destructors necessary.
162 */
163void destroy_statistic(struct statistic *s);
164
165/*
166 * This creates a callback that is invoked when destroy_statistic is called.
167 *
168 * This returns true when an error occurs.
169 */
170bool create_statistic_destructor(struct statistic *s,
171				 void (*destructor) (struct statistic *, void *),
172				 void *ptr);
173
174#endif /*STATISTIC_H*/
175