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