1/*
2 * Copyright (c) 2012, 2016 SAP SE. 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 "libperfstat_aix.hpp"
26#include "misc_aix.hpp"
27
28#include <dlfcn.h>
29#include <sys/systemcfg.h>
30
31// Handle to the libperfstat.
32static void* g_libhandle = NULL;
33
34typedef int (*fun_perfstat_cpu_total_t) (perfstat_id_t *name, PERFSTAT_CPU_TOTAL_T_LATEST* userbuff,
35                                         int sizeof_userbuff, int desired_number);
36
37typedef int (*fun_perfstat_memory_total_t) (perfstat_id_t *name, perfstat_memory_total_t* userbuff,
38                                            int sizeof_userbuff, int desired_number);
39
40typedef int (*fun_perfstat_partition_total_t) (perfstat_id_t *name,
41    PERFSTAT_PARTITON_TOTAL_T_LATEST* userbuff, int sizeof_userbuff,
42    int desired_number);
43
44typedef int (*fun_perfstat_wpar_total_t) (perfstat_id_wpar_t *name,
45    PERFSTAT_WPAR_TOTAL_T_LATEST* userbuff, int sizeof_userbuff,
46    int desired_number);
47
48typedef void (*fun_perfstat_reset_t) ();
49
50typedef cid_t (*fun_wpar_getcid_t) ();
51
52static fun_perfstat_cpu_total_t     g_fun_perfstat_cpu_total = NULL;
53static fun_perfstat_memory_total_t  g_fun_perfstat_memory_total = NULL;
54static fun_perfstat_partition_total_t g_fun_perfstat_partition_total = NULL;
55static fun_perfstat_wpar_total_t    g_fun_perfstat_wpar_total = NULL;
56static fun_perfstat_reset_t         g_fun_perfstat_reset = NULL;
57static fun_wpar_getcid_t            g_fun_wpar_getcid = NULL;
58
59bool libperfstat::init() {
60
61  // Dynamically load the libperfstat porting library.
62  g_libhandle = dlopen("/usr/lib/libperfstat.a(shr_64.o)", RTLD_MEMBER | RTLD_NOW);
63  if (!g_libhandle) {
64    trcVerbose("Cannot load libperfstat.a (dlerror: %s)", dlerror());
65    return false;
66  }
67
68  // Resolve function pointers
69
70#define RESOLVE_FUN_NO_ERROR(name) \
71  g_fun_##name = (fun_##name##_t) dlsym(g_libhandle, #name);
72
73#define RESOLVE_FUN(name) \
74  RESOLVE_FUN_NO_ERROR(name) \
75  if (!g_fun_##name) { \
76    trcVerbose("Cannot resolve " #name "() from libperfstat.a\n" \
77                      "   (dlerror: %s)", dlerror()); \
78    return false; \
79  }
80
81  // These functions may or may not be there depending on the OS release.
82  RESOLVE_FUN_NO_ERROR(perfstat_partition_total);
83  RESOLVE_FUN_NO_ERROR(perfstat_wpar_total);
84  RESOLVE_FUN_NO_ERROR(wpar_getcid);
85
86  // These functions are required for every release.
87  RESOLVE_FUN(perfstat_cpu_total);
88  RESOLVE_FUN(perfstat_memory_total);
89  RESOLVE_FUN(perfstat_reset);
90
91  trcVerbose("libperfstat loaded.");
92
93  return true;
94}
95
96void libperfstat::cleanup() {
97
98  if (g_libhandle) {
99    dlclose(g_libhandle);
100    g_libhandle = NULL;
101  }
102
103  g_fun_perfstat_cpu_total = NULL;
104  g_fun_perfstat_memory_total = NULL;
105  g_fun_perfstat_partition_total = NULL;
106  g_fun_perfstat_wpar_total = NULL;
107  g_fun_perfstat_reset = NULL;
108  g_fun_wpar_getcid = NULL;
109
110}
111
112int libperfstat::perfstat_memory_total(perfstat_id_t *name,
113                                       perfstat_memory_total_t* userbuff,
114                                       int sizeof_userbuff, int desired_number) {
115  if (g_fun_perfstat_memory_total == NULL) {
116    return -1;
117  }
118  return g_fun_perfstat_memory_total(name, userbuff, sizeof_userbuff, desired_number);
119}
120
121int libperfstat::perfstat_cpu_total(perfstat_id_t *name, PERFSTAT_CPU_TOTAL_T_LATEST* userbuff,
122                                    int sizeof_userbuff, int desired_number) {
123  if (g_fun_perfstat_cpu_total == NULL) {
124    return -1;
125  }
126  return g_fun_perfstat_cpu_total(name, userbuff, sizeof_userbuff, desired_number);
127}
128
129int libperfstat::perfstat_partition_total(perfstat_id_t *name, PERFSTAT_PARTITON_TOTAL_T_LATEST* userbuff,
130                                          int sizeof_userbuff, int desired_number) {
131  if (g_fun_perfstat_partition_total == NULL) {
132    return -1;
133  }
134  return g_fun_perfstat_partition_total(name, userbuff, sizeof_userbuff, desired_number);
135}
136
137int libperfstat::perfstat_wpar_total(perfstat_id_wpar_t *name, PERFSTAT_WPAR_TOTAL_T_LATEST* userbuff,
138                                     int sizeof_userbuff, int desired_number) {
139  if (g_fun_perfstat_wpar_total == NULL) {
140    return -1;
141  }
142  return g_fun_perfstat_wpar_total(name, userbuff, sizeof_userbuff, desired_number);
143}
144
145void libperfstat::perfstat_reset() {
146  if (g_fun_perfstat_reset != NULL) {
147    g_fun_perfstat_reset();
148  }
149}
150
151cid_t libperfstat::wpar_getcid() {
152  if (g_fun_wpar_getcid == NULL) {
153    return (cid_t) -1;
154  }
155  return g_fun_wpar_getcid();
156}
157
158
159//////////////////// convenience functions, release-independent /////////////////////////////
160
161// Excerpts from systemcfg.h definitions newer than AIX 5.3 (our oldest build platform)
162
163#define PV_6 0x100000          /* Power PC 6 */
164#define PV_6_1 0x100001        /* Power PC 6 DD1.x */
165#define PV_7 0x200000          /* Power PC 7 */
166#define PV_5_Compat 0x0F8000   /* Power PC 5 */
167#define PV_6_Compat 0x108000   /* Power PC 6 */
168#define PV_7_Compat 0x208000   /* Power PC 7 */
169#define PV_8 0x300000          /* Power PC 8 */
170#define PV_8_Compat 0x308000   /* Power PC 8 */
171
172
173// Retrieve global cpu information.
174bool libperfstat::get_cpuinfo(cpuinfo_t* pci) {
175
176  assert(pci, "get_cpuinfo: invalid parameter");
177  memset(pci, 0, sizeof(cpuinfo_t));
178
179  PERFSTAT_CPU_TOTAL_T_LATEST psct;
180  memset (&psct, '\0', sizeof(psct));
181
182  if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(PERFSTAT_CPU_TOTAL_T_LATEST), 1)) {
183    if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_71), 1)) {
184      if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_61), 1)) {
185        if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_53), 1)) {
186          trcVerbose("perfstat_cpu_total() failed (errno=%d)", errno);
187          return false;
188        }
189      }
190    }
191  }
192
193  // Global cpu information.
194  strcpy (pci->description, psct.description);
195  pci->processorHZ = psct.processorHZ;
196  pci->ncpus = psct.ncpus;
197  for (int i = 0; i < 3; i++) {
198    pci->loadavg[i] = (double) psct.loadavg[i] / (1 << SBITS);
199  }
200
201  pci->user_clock_ticks = psct.user;
202  pci->sys_clock_ticks  = psct.sys;
203  pci->idle_clock_ticks = psct.idle;
204  pci->wait_clock_ticks = psct.wait;
205
206  // Get the processor version from _system_configuration.
207  switch (_system_configuration.version) {
208  case PV_8:
209    strcpy(pci->version, "Power PC 8");
210    break;
211  case PV_7:
212    strcpy(pci->version, "Power PC 7");
213    break;
214  case PV_6_1:
215    strcpy(pci->version, "Power PC 6 DD1.x");
216    break;
217  case PV_6:
218    strcpy(pci->version, "Power PC 6");
219    break;
220  case PV_5:
221    strcpy(pci->version, "Power PC 5");
222    break;
223  case PV_5_2:
224    strcpy(pci->version, "Power PC 5_2");
225    break;
226  case PV_5_3:
227    strcpy(pci->version, "Power PC 5_3");
228    break;
229  case PV_5_Compat:
230    strcpy(pci->version, "PV_5_Compat");
231    break;
232  case PV_6_Compat:
233    strcpy(pci->version, "PV_6_Compat");
234    break;
235  case PV_7_Compat:
236    strcpy(pci->version, "PV_7_Compat");
237    break;
238  case PV_8_Compat:
239    strcpy(pci->version, "PV_8_Compat");
240    break;
241  default:
242    strcpy(pci->version, "unknown");
243  }
244
245  return true;
246}
247
248// Retrieve partition information.
249bool libperfstat::get_partitioninfo(partitioninfo_t* ppi) {
250
251  assert(ppi, "get_partitioninfo: invalid parameter");
252  memset(ppi, 0, sizeof(partitioninfo_t));
253
254  PERFSTAT_PARTITON_TOTAL_T_LATEST pspt;
255  memset(&pspt, '\0', sizeof(pspt));
256
257  bool ame_details = true;
258
259  if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(PERFSTAT_PARTITON_TOTAL_T_LATEST), 1)) {
260    if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_71), 1)) {
261      ame_details = false;
262      if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_61), 1)) {
263        if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_53), 1)) {
264          if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_53_5), 1)) {
265            trcVerbose("perfstat_partition_total() failed (errno=%d)", errno);
266            return false;
267          }
268        }
269      }
270    }
271  }
272
273  // partition type info
274  ppi->shared_enabled = pspt.type.b.shared_enabled;
275  ppi->smt_capable = pspt.type.b.smt_capable;
276  ppi->smt_enabled = pspt.type.b.smt_enabled;
277  ppi->lpar_capable = pspt.type.b.lpar_capable;
278  ppi->lpar_enabled = pspt.type.b.lpar_enabled;
279  ppi->dlpar_capable = pspt.type.b.dlpar_capable;
280  ppi->capped = pspt.type.b.capped;
281  ppi->kernel_is_64 = pspt.type.b.kernel_is_64;
282  ppi->pool_util_authority = pspt.type.b.pool_util_authority;
283  ppi->donate_capable = pspt.type.b.donate_capable;
284  ppi->donate_enabled = pspt.type.b.donate_enabled;
285  ppi->ams_capable = pspt.type.b.ams_capable;
286  ppi->ams_enabled = pspt.type.b.ams_enabled;
287  ppi->power_save = pspt.type.b.power_save;
288  ppi->ame_enabled = pspt.type.b.ame_enabled;
289
290  // partition total info
291  ppi->online_cpus = pspt.online_cpus;
292  ppi->entitled_proc_capacity = pspt.entitled_proc_capacity;
293  ppi->var_proc_capacity_weight = pspt.var_proc_capacity_weight;
294  ppi->phys_cpus_pool = pspt.phys_cpus_pool;
295  ppi->pool_id = pspt.pool_id;
296  ppi->entitled_pool_capacity = pspt.entitled_pool_capacity;
297  strcpy(ppi->name, pspt.name);
298
299  // Added values to ppi that we need for later computation of cpu utilization
300  // ( pool authorization needed for pool_idle_time ??? )
301  ppi->timebase_last   = pspt.timebase_last;
302  ppi->pool_idle_time  = pspt.pool_idle_time;
303  ppi->pcpu_tics_user  = pspt.puser;
304  ppi->pcpu_tics_sys   = pspt.psys;
305  ppi->pcpu_tics_idle  = pspt.pidle;
306  ppi->pcpu_tics_wait  = pspt.pwait;
307
308  // Additional AME information.
309  if (ame_details) {
310    ppi->true_memory = pspt.true_memory * 4096;
311    ppi->expanded_memory = pspt.expanded_memory * 4096;
312    ppi->target_memexp_factr = pspt.target_memexp_factr;
313    ppi->current_memexp_factr = pspt.current_memexp_factr;
314    ppi->cmcs_total_time = pspt.cmcs_total_time;
315  }
316
317  return true;
318}
319
320// Retrieve wpar information.
321bool libperfstat::get_wparinfo(wparinfo_t* pwi) {
322
323  assert(pwi, "get_wparinfo: invalid parameter");
324  memset(pwi, 0, sizeof(wparinfo_t));
325
326  if (libperfstat::wpar_getcid() <= 0) {
327    return false;
328  }
329
330  PERFSTAT_WPAR_TOTAL_T_LATEST pswt;
331  memset (&pswt, '\0', sizeof(pswt));
332
333  if (-1 == libperfstat::perfstat_wpar_total(NULL, &pswt, sizeof(PERFSTAT_WPAR_TOTAL_T_LATEST), 1)) {
334    if (-1 == libperfstat::perfstat_wpar_total(NULL, &pswt, sizeof(perfstat_wpar_total_t_61), 1)) {
335      trcVerbose("perfstat_wpar_total() failed (errno=%d)", errno);
336      return false;
337    }
338  }
339
340  // WPAR type info.
341  pwi->app_wpar = pswt.type.b.app_wpar;
342  pwi->cpu_rset = pswt.type.b.cpu_rset;
343  pwi->cpu_xrset = pswt.type.b.cpu_xrset;
344  pwi->cpu_limits = pswt.type.b.cpu_limits;
345  pwi->mem_limits = pswt.type.b.mem_limits;
346  // WPAR total info.
347  strcpy(pwi->name, pswt.name);
348  pwi->wpar_id = pswt.wpar_id;
349  pwi->cpu_limit = pswt.cpu_limit;
350  pwi->mem_limit = pswt.mem_limit;
351
352  return true;
353}
354