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 "libperfstat_aix.hpp"
  26 #include "misc_aix.hpp"
  27 
  28 #include <dlfcn.h>
  29 #include <sys/systemcfg.h>
  30 
  31 // Handle to the libperfstat.
  32 static void* g_libhandle = NULL;
  33 
  34 typedef int (*fun_perfstat_cpu_total_t) (perfstat_id_t *name, PERFSTAT_CPU_TOTAL_T_LATEST* userbuff,
  35                                          int sizeof_userbuff, int desired_number);
  36 
  37 typedef int (*fun_perfstat_memory_total_t) (perfstat_id_t *name, perfstat_memory_total_t* userbuff,
  38                                             int sizeof_userbuff, int desired_number);
  39 
  40 typedef 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 
  44 typedef 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 
  48 typedef void (*fun_perfstat_reset_t) ();
  49 
  50 typedef cid_t (*fun_wpar_getcid_t) ();
  51 
  52 static fun_perfstat_cpu_total_t     g_fun_perfstat_cpu_total = NULL;
  53 static fun_perfstat_memory_total_t  g_fun_perfstat_memory_total = NULL;
  54 static fun_perfstat_partition_total_t g_fun_perfstat_partition_total = NULL;
  55 static fun_perfstat_wpar_total_t    g_fun_perfstat_wpar_total = NULL;
  56 static fun_perfstat_reset_t         g_fun_perfstat_reset = NULL;
  57 static fun_wpar_getcid_t            g_fun_wpar_getcid = NULL;
  58 
  59 bool 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 
  96 void 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 
 112 int 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 
 121 int 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 
 129 int libperfstat::perfstat_partition_total(perfstat_id_t *name, PERFSTAT_PARTITON_TOTAL_T_LATEST* userbuff,
 130     int sizeof_userbuff, int desired_number)
 131 {
 132   if (g_fun_perfstat_partition_total == NULL) {
 133     return -1;
 134   }
 135   return g_fun_perfstat_partition_total(name, userbuff, sizeof_userbuff, desired_number);
 136 }
 137 
 138 int libperfstat::perfstat_wpar_total(perfstat_id_wpar_t *name, PERFSTAT_WPAR_TOTAL_T_LATEST* userbuff,
 139     int sizeof_userbuff, int desired_number)
 140 {
 141   if (g_fun_perfstat_wpar_total == NULL) {
 142     return -1;
 143   }
 144   return g_fun_perfstat_wpar_total(name, userbuff, sizeof_userbuff, desired_number);
 145 }
 146 
 147 void libperfstat::perfstat_reset() {
 148   if (g_fun_perfstat_reset != NULL) {
 149     g_fun_perfstat_reset();
 150   }
 151 }
 152 
 153 cid_t libperfstat::wpar_getcid() {
 154   if (g_fun_wpar_getcid == NULL) {
 155     return (cid_t) -1;
 156   }
 157   return g_fun_wpar_getcid();
 158 }
 159 
 160 
 161 //////////////////// convenience functions, release-independent /////////////////////////////
 162 
 163 // Excerpts from systemcfg.h definitions newer than AIX 5.2 and XLC 8
 164 // SAPJVM MBS 2012-08-14 : on AIX 5.3/ xlC10 some of the definitions are available but
 165 // e.g. PV_/ stuff is still missing so keep the definitions
 166 //
 167 // XLC 8 build is using the systemcfg.h from /usr/vacpp/include/sys instead of /usr/include/sys
 168 // Missing values for the version field
 169 #define PV_5_2 0x0F0001        /* Power PC 5 */
 170 #define PV_5_3 0x0F0002        /* Power PC 5 */
 171 #define PV_6 0x100000          /* Power PC 6 */
 172 #define PV_6_1 0x100001        /* Power PC 6 DD1.x */
 173 #define PV_7 0x200000          /* Power PC 7 */
 174 #define PV_5_Compat 0x0F8000   /* Power PC 5 */
 175 #define PV_6_Compat 0x108000   /* Power PC 6 */
 176 #define PV_7_Compat 0x208000   /* Power PC 7 */
 177 #define PV_8 0x300000          /* Power PC 8 */
 178 #define PV_8_Compat 0x308000   /* Power PC 8 */
 179 
 180 
 181 // Retrieve global cpu information.
 182 bool libperfstat::get_cpuinfo(cpuinfo_t* pci) {
 183 
 184   assert(pci, "get_cpuinfo: invalid parameter");
 185   memset(pci, 0, sizeof(cpuinfo_t));
 186 
 187   PERFSTAT_CPU_TOTAL_T_LATEST psct;
 188   memset (&psct, '\0', sizeof(psct));
 189 
 190   if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(PERFSTAT_CPU_TOTAL_T_LATEST), 1)) {
 191     if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_61), 1)) {
 192       if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_53), 1)) {
 193         if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t_52), 1)) {
 194           trcVerbose("perfstat_cpu_total() failed (errno=%d)", errno);
 195           return false;
 196         }
 197       }
 198     }
 199   }
 200 
 201   // Global cpu information.
 202   strcpy (pci->description, psct.description);
 203   pci->processorHZ = psct.processorHZ;
 204   pci->ncpus = psct.ncpus;
 205   for (int i = 0; i < 3; i++) {
 206     pci->loadavg[i] = (double) psct.loadavg[i] / (1 << SBITS);
 207   }
 208 
 209   pci->user_clock_ticks = psct.user;
 210   pci->sys_clock_ticks  = psct.sys;
 211   pci->idle_clock_ticks = psct.idle;
 212   pci->wait_clock_ticks = psct.wait;
 213 
 214   // Get the processor version from _system_configuration.
 215   switch (_system_configuration.version) {
 216   case PV_8:
 217     strcpy(pci->version, "Power PC 8");
 218     break;
 219   case PV_7:
 220     strcpy(pci->version, "Power PC 7");
 221     break;
 222   case PV_6_1:
 223     strcpy(pci->version, "Power PC 6 DD1.x");
 224     break;
 225   case PV_6:
 226     strcpy(pci->version, "Power PC 6");
 227     break;
 228   case PV_5:
 229     strcpy(pci->version, "Power PC 5");
 230     break;
 231   case PV_5_2:
 232     strcpy(pci->version, "Power PC 5_2");
 233     break;
 234   case PV_5_3:
 235     strcpy(pci->version, "Power PC 5_3");
 236     break;
 237   case PV_5_Compat:
 238     strcpy(pci->version, "PV_5_Compat");
 239     break;
 240   case PV_6_Compat:
 241     strcpy(pci->version, "PV_6_Compat");
 242     break;
 243   case PV_7_Compat:
 244     strcpy(pci->version, "PV_7_Compat");
 245     break;
 246   case PV_8_Compat:
 247     strcpy(pci->version, "PV_8_Compat");
 248     break;
 249   default:
 250     strcpy(pci->version, "unknown");
 251   }
 252 
 253   return true;
 254 
 255 }
 256 
 257 // Retrieve partition information.
 258 bool libperfstat::get_partitioninfo(partitioninfo_t* ppi) {
 259 
 260   assert(ppi, "get_partitioninfo: invalid parameter");
 261   memset(ppi, 0, sizeof(partitioninfo_t));
 262 
 263   PERFSTAT_PARTITON_TOTAL_T_LATEST pspt;
 264   memset(&pspt, '\0', sizeof(pspt));
 265 
 266   bool ame_details = true;
 267 
 268   if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(PERFSTAT_PARTITON_TOTAL_T_LATEST), 1)) {
 269       if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_71), 1)) {
 270           ame_details = false;
 271           if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_61), 1)) {
 272               if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_53), 1)) {
 273                   if (-1 == libperfstat::perfstat_partition_total(NULL, &pspt, sizeof(perfstat_partition_total_t_53_5), 1)) {
 274                       trcVerbose("perfstat_partition_total() failed (errno=%d)", errno);
 275                       return false;
 276                   }
 277               }
 278           }
 279       }
 280   }
 281 
 282   // partition type info
 283   ppi->shared_enabled = pspt.type.b.shared_enabled;
 284   ppi->smt_capable = pspt.type.b.smt_capable;
 285   ppi->smt_enabled = pspt.type.b.smt_enabled;
 286   ppi->lpar_capable = pspt.type.b.lpar_capable;
 287   ppi->lpar_enabled = pspt.type.b.lpar_enabled;
 288   ppi->dlpar_capable = pspt.type.b.dlpar_capable;
 289   ppi->capped = pspt.type.b.capped;
 290   ppi->kernel_is_64 = pspt.type.b.kernel_is_64;
 291   ppi->pool_util_authority = pspt.type.b.pool_util_authority;
 292   ppi->donate_capable = pspt.type.b.donate_capable;
 293   ppi->donate_enabled = pspt.type.b.donate_enabled;
 294   ppi->ams_capable = pspt.type.b.ams_capable;
 295   ppi->ams_enabled = pspt.type.b.ams_enabled;
 296   ppi->power_save = pspt.type.b.power_save;
 297   ppi->ame_enabled = pspt.type.b.ame_enabled;
 298 
 299   // Partition total info.
 300   ppi->online_cpus = pspt.online_cpus;
 301   ppi->entitled_proc_capacity = pspt.entitled_proc_capacity;
 302   ppi->var_proc_capacity_weight = pspt.var_proc_capacity_weight;
 303   ppi->phys_cpus_pool = pspt.phys_cpus_pool;
 304   ppi->pool_id = pspt.pool_id;
 305   ppi->entitled_pool_capacity = pspt.entitled_pool_capacity;
 306   strcpy(ppi->name, pspt.name);
 307 
 308   // Added values to ppi that we need for later computation of cpu utilization
 309   // ( pool authorization needed for pool_idle_time ??? )
 310   ppi->timebase_last   = pspt.timebase_last;
 311   ppi->pool_idle_time  = pspt.pool_idle_time;
 312   ppi->pcpu_tics_user  = pspt.puser;
 313   ppi->pcpu_tics_sys   = pspt.psys;
 314   ppi->pcpu_tics_idle  = pspt.pidle;
 315   ppi->pcpu_tics_wait  = pspt.pwait;
 316 
 317   // Additional AME information.
 318   if(ame_details) {
 319       ppi->true_memory = pspt.true_memory * 4096;
 320       ppi->expanded_memory = pspt.expanded_memory * 4096;
 321       ppi->target_memexp_factr = pspt.target_memexp_factr;
 322       ppi->current_memexp_factr = pspt.current_memexp_factr;
 323       ppi->cmcs_total_time = pspt.cmcs_total_time;
 324   }
 325 
 326   return true;
 327 
 328 }
 329 
 330 // Retrieve wpar information.
 331 bool libperfstat::get_wparinfo(wparinfo_t* pwi) {
 332 
 333   assert(pwi, "get_wparinfo: invalid parameter");
 334   memset(pwi, 0, sizeof(wparinfo_t));
 335 
 336   if (libperfstat::wpar_getcid() <= 0) {
 337     return false;
 338   }
 339 
 340   PERFSTAT_WPAR_TOTAL_T_LATEST pswt;
 341   memset (&pswt, '\0', sizeof(pswt));
 342 
 343   if (-1 == libperfstat::perfstat_wpar_total(NULL, &pswt, sizeof(PERFSTAT_WPAR_TOTAL_T_LATEST), 1)) {
 344     if (-1 == libperfstat::perfstat_wpar_total(NULL, &pswt, sizeof(perfstat_wpar_total_t_61), 1)) {
 345       trcVerbose("perfstat_wpar_total() failed (errno=%d)", errno);
 346       return false;
 347     }
 348   }
 349 
 350   // WPAR type info.
 351   pwi->app_wpar = pswt.type.b.app_wpar;
 352   pwi->cpu_rset = pswt.type.b.cpu_rset;
 353   pwi->cpu_xrset = pswt.type.b.cpu_xrset;
 354   pwi->cpu_limits = pswt.type.b.cpu_limits;
 355   pwi->mem_limits = pswt.type.b.mem_limits;
 356   // WPAR total info.
 357   strcpy(pwi->name, pswt.name);
 358   pwi->wpar_id = pswt.wpar_id;
 359   pwi->cpu_limit = pswt.cpu_limit;
 360   pwi->mem_limit = pswt.mem_limit;
 361 
 362   return true;
 363 
 364 }
 365 
 366 
 367 
 368