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 <string.h>
24#include <stdlib.h>
25#include "pid.h"
26#include "libtop.h"
27#include "generic.h"
28
29static void get_pid_suffix(const libtop_psamp_t *psamp, char *suffix,
30			   size_t length) {
31    bool proc_is_foreign = false;
32    bool proc_is_64 = false;
33    bool host_is_64 = false;
34
35#if defined(__LP64__)
36    host_is_64 = true;
37#endif
38
39    switch (psamp->cputype) {
40    case CPU_TYPE_X86_64:
41	proc_is_64 = true;
42	// FALLTHROUGH
43    case CPU_TYPE_X86:
44#if !defined(__i386__) && !defined(__x86_64__)
45	proc_is_foreign = true;
46#endif
47	break;
48    case CPU_TYPE_POWERPC64:
49	proc_is_64 = true;
50	// FALLTHROUGH
51    case CPU_TYPE_POWERPC:
52#if !defined(__ppc__) && !defined(__ppc64__)
53	proc_is_foreign = true;
54#endif
55	break;
56    case CPU_TYPE_ARM:
57#if !defined(__arm__)
58	proc_is_foreign = true;
59#endif
60	break;
61    default:
62	proc_is_foreign = true;
63	break;
64    }
65
66    if(proc_is_foreign) {
67	strncpy(suffix, "*", length);
68    } else if(host_is_64 && !proc_is_64) {
69	strncpy(suffix, "-", length);
70    } else {
71	strncpy(suffix, " ", length);
72    }
73}
74
75
76static bool pid_insert_cell(struct statistic *s, const void *sample) {
77    const libtop_psamp_t *psamp = sample;
78    char buf[GENERIC_INT_SIZE + 2];
79    char suffix[2];
80    unsigned int p;
81
82    get_pid_suffix(psamp, suffix, sizeof(suffix));
83    p = (unsigned int)psamp->pid;
84
85    if(-1 == snprintf(buf, sizeof(buf), "%u%s", p, suffix))
86	return true;
87
88    return generic_insert_cell(s, buf);
89}
90
91static struct statistic_callbacks callbacks = {
92    .draw = generic_draw,
93    .resize_cells = generic_resize_cells,
94    .move_cells = generic_move_cells,
95    .get_request_size = generic_get_request_size,
96    .get_minimum_size = generic_get_minimum_size,
97    .insert_cell = pid_insert_cell,
98    .reset_insertion = generic_reset_insertion
99};
100
101struct statistic *top_pid_create(WINDOW *parent, const char *name) {
102    return create_statistic(STATISTIC_PID, parent, NULL, &callbacks, name);
103}
104