libperfstat_aix.cpp revision 5969:666e6ce3976c
1/*
2 * Copyright 2012, 2013 SAP AG. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "runtime/arguments.hpp"
26#include "libperfstat_aix.hpp"
27
28// For dlopen and friends
29#include <fcntl.h>
30
31// handle to the libperfstat
32static void* g_libhandle = NULL;
33
34// whether initialization worked
35static bool g_initialized = false;
36
37
38typedef int (*fun_perfstat_cpu_total_t) (perfstat_id_t *name, perfstat_cpu_total_t* userbuff,
39                                         int sizeof_userbuff, int desired_number);
40
41typedef int (*fun_perfstat_memory_total_t) (perfstat_id_t *name, perfstat_memory_total_t* userbuff,
42                                            int sizeof_userbuff, int desired_number);
43
44typedef void (*fun_perfstat_reset_t) ();
45
46static fun_perfstat_cpu_total_t     g_fun_perfstat_cpu_total = NULL;
47static fun_perfstat_memory_total_t  g_fun_perfstat_memory_total = NULL;
48static fun_perfstat_reset_t         g_fun_perfstat_reset = NULL;
49
50bool libperfstat::init() {
51
52  if (g_initialized) {
53    return true;
54  }
55
56  g_initialized = false;
57
58  // dynamically load the libperfstat porting library.
59  g_libhandle = dlopen("/usr/lib/libperfstat.a(shr_64.o)", RTLD_MEMBER | RTLD_NOW);
60  if (!g_libhandle) {
61    if (Verbose) {
62      fprintf(stderr, "Cannot load libperfstat.a (dlerror: %s)", dlerror());
63    }
64    return false;
65  }
66
67  // resolve function pointers
68
69#define RESOLVE_FUN_NO_ERROR(name) \
70  g_fun_##name = (fun_##name##_t) dlsym(g_libhandle, #name);
71
72#define RESOLVE_FUN(name) \
73  RESOLVE_FUN_NO_ERROR(name) \
74  if (!g_fun_##name) { \
75    if (Verbose) { \
76      fprintf(stderr, "Cannot resolve " #name "() from libperfstat.a\n" \
77                      "   (dlerror: %s)", dlerror()); \
78      } \
79    return false; \
80  }
81
82  RESOLVE_FUN(perfstat_cpu_total);
83  RESOLVE_FUN(perfstat_memory_total);
84  RESOLVE_FUN(perfstat_reset);
85
86  g_initialized = true;
87
88  return true;
89}
90
91void libperfstat::cleanup() {
92
93  g_initialized = false;
94
95  if (g_libhandle) {
96    dlclose(g_libhandle);
97    g_libhandle = NULL;
98  }
99
100  g_fun_perfstat_cpu_total = NULL;
101  g_fun_perfstat_memory_total = NULL;
102  g_fun_perfstat_reset = NULL;
103}
104
105int libperfstat::perfstat_memory_total(perfstat_id_t *name,
106                                       perfstat_memory_total_t* userbuff,
107                                       int sizeof_userbuff, int desired_number) {
108  assert(g_initialized, "libperfstat not initialized");
109  assert(g_fun_perfstat_memory_total, "");
110  return g_fun_perfstat_memory_total(name, userbuff, sizeof_userbuff, desired_number);
111}
112
113int libperfstat::perfstat_cpu_total(perfstat_id_t *name, perfstat_cpu_total_t* userbuff,
114                                    int sizeof_userbuff, int desired_number) {
115  assert(g_initialized, "libperfstat not initialized");
116  assert(g_fun_perfstat_cpu_total, "");
117  return g_fun_perfstat_cpu_total(name, userbuff, sizeof_userbuff, desired_number);
118}
119
120void libperfstat::perfstat_reset() {
121  assert(g_initialized, "libperfstat not initialized");
122  assert(g_fun_perfstat_reset, "");
123  g_fun_perfstat_reset();
124}
125