Add statistic interface for cx.
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 21 May 2008 09:59:49 +0000 (10:59 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 21 May 2008 09:59:49 +0000 (10:59 +0100)
Implement statistic interface for cx via sysctl & libxc. Provide a
easy way to collect processor cx info within dom0.

Signed-off-by: Wei Gang <gang.wei@intel.com>
tools/libxc/xc_pm.c
tools/libxc/xenctrl.h
xen/arch/x86/acpi/cpu_idle.c
xen/arch/x86/acpi/pmstat.c
xen/include/public/sysctl.h

index 0378cbd521b607607e07211d44e904781e17fa1c..2737cbe31284624b01d61d8ae1b2f704ccbd3292 100644 (file)
@@ -99,3 +99,71 @@ int xc_pm_reset_pxstat(int xc_handle, int cpuid)
 
     return xc_sysctl(xc_handle, &sysctl);
 }
+
+int xc_pm_get_max_cx(int xc_handle, int cpuid, int *max_cx)
+{
+    DECLARE_SYSCTL;
+    int ret = 0;
+
+    sysctl.cmd = XEN_SYSCTL_get_pmstat;
+    sysctl.u.get_pmstat.type = PMSTAT_get_max_cx;
+    sysctl.u.get_pmstat.cpuid = cpuid;
+    if ( (ret = xc_sysctl(xc_handle, &sysctl)) != 0 )
+        return ret;
+
+    *max_cx = sysctl.u.get_pmstat.u.getcx.nr;
+    return ret;
+}
+
+int xc_pm_get_cxstat(int xc_handle, int cpuid, struct xc_cx_stat *cxpt)
+{
+    DECLARE_SYSCTL;
+    int max_cx, ret;
+
+    if( !cxpt || !(cxpt->triggers) || !(cxpt->residencies) )
+        return -EINVAL;
+
+    if ( (ret = xc_pm_get_max_cx(xc_handle, cpuid, &max_cx)) )
+        goto unlock_0;
+
+    if ( (ret = lock_pages(cxpt, sizeof(struct xc_cx_stat))) )
+        goto unlock_0;
+    if ( (ret = lock_pages(cxpt->triggers, max_cx * sizeof(uint64_t))) )
+        goto unlock_1;
+    if ( (ret = lock_pages(cxpt->residencies, max_cx * sizeof(uint64_t))) )
+        goto unlock_2;
+
+    sysctl.cmd = XEN_SYSCTL_get_pmstat;
+    sysctl.u.get_pmstat.type = PMSTAT_get_cxstat;
+    sysctl.u.get_pmstat.cpuid = cpuid;
+    set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.triggers, cxpt->triggers);
+    set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.residencies, 
+                         cxpt->residencies);
+
+    if ( (ret = xc_sysctl(xc_handle, &sysctl)) )
+        goto unlock_3;
+
+    cxpt->nr = sysctl.u.get_pmstat.u.getcx.nr;
+    cxpt->last = sysctl.u.get_pmstat.u.getcx.last;
+    cxpt->idle_time = sysctl.u.get_pmstat.u.getcx.idle_time;
+
+unlock_3:
+    unlock_pages(cxpt->residencies, max_cx * sizeof(uint64_t));
+unlock_2:
+    unlock_pages(cxpt->triggers, max_cx * sizeof(uint64_t));
+unlock_1:
+    unlock_pages(cxpt, sizeof(struct xc_cx_stat));
+unlock_0:
+    return ret;
+}
+
+int xc_pm_reset_cxstat(int xc_handle, int cpuid)
+{
+    DECLARE_SYSCTL;
+
+    sysctl.cmd = XEN_SYSCTL_get_pmstat;
+    sysctl.u.get_pmstat.type = PMSTAT_reset_cxstat;
+    sysctl.u.get_pmstat.cpuid = cpuid;
+
+    return xc_sysctl(xc_handle, &sysctl);
+}
index 130a05e19d04ccbcb32bee0b3ca6d0be605af85f..1e03025ca6a00f2e9db1d339494b6264d6cb6652 100644 (file)
@@ -1053,4 +1053,17 @@ int xc_pm_get_max_px(int xc_handle, int cpuid, int *max_px);
 int xc_pm_get_pxstat(int xc_handle, int cpuid, struct xc_px_stat *pxpt);
 int xc_pm_reset_pxstat(int xc_handle, int cpuid);
 
+struct xc_cx_stat {
+    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
+    uint32_t last;         /* last Cx state */
+    uint64_t idle_time;    /* idle time from boot */
+    uint64_t *triggers;    /* Cx trigger counts */
+    uint64_t *residencies; /* Cx residencies */
+};
+typedef struct xc_cx_stat xc_cx_stat_t;
+
+int xc_pm_get_max_cx(int xc_handle, int cpuid, int *max_cx);
+int xc_pm_get_cxstat(int xc_handle, int cpuid, struct xc_cx_stat *cxpt);
+int xc_pm_reset_cxstat(int xc_handle, int cpuid);
+
 #endif /* XENCTRL_H */
index f7d615fb42113fc58f1d38cd18ee35a76182be8c..0d471d9a7da6e292d541c3d3bd944e21d9fe4e8e 100644 (file)
@@ -44,6 +44,7 @@
 #include <asm/hpet.h>
 #include <asm/processor.h>
 #include <public/platform.h>
+#include <public/sysctl.h>
 
 #define DEBUG_PM_CX
 
@@ -940,3 +941,41 @@ long set_cx_pminfo(uint32_t cpu, struct xen_processor_power *power)
         
     return 0;
 }
+
+uint32_t pmstat_get_cx_nr(uint32_t cpuid)
+{
+    return processor_powers[cpuid].count;
+}
+
+int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat)
+{
+    struct acpi_processor_power *power = &processor_powers[cpuid];
+    struct vcpu *v = idle_vcpu[cpuid];
+    uint64_t usage;
+    int i;
+
+    stat->last = (power->state) ? power->state->type : 0;
+    stat->nr = processor_powers[cpuid].count;
+    stat->idle_time = v->runstate.time[RUNSTATE_running];
+    if ( v->is_running )
+        stat->idle_time += NOW() - v->runstate.state_entry_time;
+
+    for ( i = 0; i < power->count; i++ )
+    {
+        usage = power->states[i].usage;
+        if ( copy_to_guest_offset(stat->triggers, i, &usage, 1) )
+            return -EFAULT;
+    }
+    for ( i = 0; i < power->count; i++ )
+        if ( copy_to_guest_offset(stat->residencies, i, 
+                                  &power->states[i].time, 1) )
+            return -EFAULT;
+
+    return 0;
+}
+
+int pmstat_reset_cx_stat(uint32_t cpuid)
+{
+    return 0;
+}
+
index a18a78e69f13e04395d3f4acc5c1bc13c945539d..9aa5a4750a3e64553e58090be0712db51c3ba706 100644 (file)
 
 struct pm_px px_statistic_data[NR_CPUS];
 
+extern uint32_t pmstat_get_cx_nr(uint32_t cpuid);
+extern int pmstat_get_cx_stat(uint32_t cpuid, struct pm_cx_stat *stat);
+extern int pmstat_reset_cx_stat(uint32_t cpuid);
+
 int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 {
     int ret = 0;
@@ -50,7 +54,7 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 
     /* to protect the case when Px was controlled by dom0-kernel */
     /* or when CPU_FREQ not set in which case ACPI Px objects not parsed */
-    if ( !pmpt->perf.init )
+    if ( !pmpt->perf.init && (op->type & PMSTAT_CATEGORY_MASK) == PMSTAT_PX )
         return -EINVAL;
 
     if ( !cpu_online(op->cpuid) )
@@ -100,6 +104,25 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
         break;
     }
 
+    case PMSTAT_get_max_cx:
+    {
+        op->u.getcx.nr = pmstat_get_cx_nr(op->cpuid);
+        ret = 0;
+        break;
+    }
+
+    case PMSTAT_get_cxstat:
+    {
+        ret = pmstat_get_cx_stat(op->cpuid, &op->u.getcx);
+        break;
+    }
+
+    case PMSTAT_reset_cxstat:
+    {
+        ret = pmstat_reset_cx_stat(op->cpuid);
+        break;
+    }
+
     default:
         printk("not defined sub-hypercall @ do_get_pm_info\n");
         ret = -ENOSYS;
index 0a2c55b7cf7b7739244e7a6c94742992aa661a8c..76998c946f524cd9251cb68157db4012ff417063 100644 (file)
@@ -233,15 +233,30 @@ struct pm_px_stat {
 typedef struct pm_px_stat pm_px_stat_t;
 DEFINE_XEN_GUEST_HANDLE(pm_px_stat_t);
 
+struct pm_cx_stat {
+    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
+    uint32_t last;  /* last Cx state */
+    uint64_aligned_t idle_time;                 /* idle time from boot */
+    XEN_GUEST_HANDLE_64(uint64) triggers;    /* Cx trigger counts */
+    XEN_GUEST_HANDLE_64(uint64) residencies; /* Cx residencies */
+};
+
 struct xen_sysctl_get_pmstat {
-#define PMSTAT_get_max_px   0x11
-#define PMSTAT_get_pxstat   0x12
-#define PMSTAT_reset_pxstat 0x13
+#define PMSTAT_CATEGORY_MASK 0xf0
+#define PMSTAT_PX            0x10
+#define PMSTAT_CX            0x20
+#define PMSTAT_get_max_px    (PMSTAT_PX | 0x1)
+#define PMSTAT_get_pxstat    (PMSTAT_PX | 0x2)
+#define PMSTAT_reset_pxstat  (PMSTAT_PX | 0x3)
+#define PMSTAT_get_max_cx    (PMSTAT_CX | 0x1)
+#define PMSTAT_get_cxstat    (PMSTAT_CX | 0x2)
+#define PMSTAT_reset_cxstat  (PMSTAT_CX | 0x3)
     uint32_t type;
     uint32_t cpuid;
     union {
         struct pm_px_stat getpx;
-        /* other struct for cx, tx, etc */
+        struct pm_cx_stat getcx;
+        /* other struct for tx, etc */
     } u;
 };
 typedef struct xen_sysctl_get_pmstat xen_sysctl_get_pmstat_t;