Author: jhb
Date: Tue May 19 18:58:18 2015
New Revision: 283120
URL: https://svnweb.freebsd.org/changeset/base/283120

Log:
  Use fixed enum values for PMC_CLASSES().
  
  This removes one of the frequent causes of ABI breakage when new CPU
  types are added to hwpmc(4).
  
  Differential Revision:        https://reviews.freebsd.org/D2586
  Reviewed by:  davide, emaste, gnn (earlier version)
  MFC after:    2 weeks

Modified:
  head/lib/libpmc/libpmc.c
  head/sys/dev/hwpmc/hwpmc_mod.c
  head/sys/sys/pmc.h

Modified: head/lib/libpmc/libpmc.c
==============================================================================
--- head/lib/libpmc/libpmc.c    Tue May 19 18:35:18 2015        (r283119)
+++ head/lib/libpmc/libpmc.c    Tue May 19 18:58:18 2015        (r283120)
@@ -423,9 +423,14 @@ static const char * pmc_capability_names
        __PMC_CAPS()
 };
 
-static const char * pmc_class_names[] = {
+struct pmc_class_map {
+       enum pmc_class  pm_class;
+       const char      *pm_name;
+};
+
+static const struct pmc_class_map pmc_class_names[] = {
 #undef __PMC_CLASS
-#define __PMC_CLASS(C) #C ,
+#define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
        __PMC_CLASSES()
 };
 
@@ -3362,9 +3367,11 @@ pmc_name_of_capability(enum pmc_caps cap
 const char *
 pmc_name_of_class(enum pmc_class pc)
 {
-       if ((int) pc >= PMC_CLASS_FIRST &&
-           pc <= PMC_CLASS_LAST)
-               return (pmc_class_names[pc]);
+       size_t n;
+
+       for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
+               if (pc == pmc_class_names[n].pm_class)
+                       return (pmc_class_names[n].pm_name);
 
        errno = EINVAL;
        return (NULL);

Modified: head/sys/dev/hwpmc/hwpmc_mod.c
==============================================================================
--- head/sys/dev/hwpmc/hwpmc_mod.c      Tue May 19 18:35:18 2015        
(r283119)
+++ head/sys/dev/hwpmc/hwpmc_mod.c      Tue May 19 18:58:18 2015        
(r283120)
@@ -4625,12 +4625,20 @@ pmc_kld_unload(void *arg __unused, const
 /*
  * initialization
  */
+static const char *
+pmc_name_of_pmcclass(enum pmc_class class)
+{
 
-static const char *pmc_name_of_pmcclass[] = {
+       switch (class) {
 #undef __PMC_CLASS
-#define        __PMC_CLASS(N) #N ,
-       __PMC_CLASSES()
-};
+#define        __PMC_CLASS(S,V,D)                                              
\
+       case PMC_CLASS_##S:                                             \
+               return #S;
+       __PMC_CLASSES();
+       default:
+               return ("<unknown>");
+       }
+}
 
 /*
  * Base class initializer: allocate structure and set default classes.
@@ -4909,7 +4917,7 @@ pmc_initialize(void)
                for (n = 0; n < (int) md->pmd_nclass; n++) {
                        pcd = &md->pmd_classdep[n];
                        printf(" %s/%d/%d/0x%b",
-                           pmc_name_of_pmcclass[pcd->pcd_class],
+                           pmc_name_of_pmcclass(pcd->pcd_class),
                            pcd->pcd_num,
                            pcd->pcd_width,
                            pcd->pcd_caps,

Modified: head/sys/sys/pmc.h
==============================================================================
--- head/sys/sys/pmc.h  Tue May 19 18:35:18 2015        (r283119)
+++ head/sys/sys/pmc.h  Tue May 19 18:58:18 2015        (r283120)
@@ -123,30 +123,30 @@ enum pmc_cputype {
  */
 
 #define        __PMC_CLASSES()                                                 
\
-       __PMC_CLASS(TSC)        /* CPU Timestamp counter */             \
-       __PMC_CLASS(K7)         /* AMD K7 performance counters */       \
-       __PMC_CLASS(K8)         /* AMD K8 performance counters */       \
-       __PMC_CLASS(P5)         /* Intel Pentium counters */            \
-       __PMC_CLASS(P6)         /* Intel Pentium Pro counters */        \
-       __PMC_CLASS(P4)         /* Intel Pentium-IV counters */         \
-       __PMC_CLASS(IAF)        /* Intel Core2/Atom, fixed function */  \
-       __PMC_CLASS(IAP)        /* Intel Core...Atom, programmable */   \
-       __PMC_CLASS(UCF)        /* Intel Uncore fixed function */       \
-       __PMC_CLASS(UCP)        /* Intel Uncore programmable */         \
-       __PMC_CLASS(XSCALE)     /* Intel XScale counters */             \
-       __PMC_CLASS(ARMV7)      /* ARMv7 */                             \
-       __PMC_CLASS(ARMV8)      /* ARMv8 */                             \
-       __PMC_CLASS(MIPS24K)    /* MIPS 24K */                          \
-       __PMC_CLASS(OCTEON)     /* Cavium Octeon */                     \
-       __PMC_CLASS(MIPS74K)    /* MIPS 74K */                          \
-       __PMC_CLASS(PPC7450)    /* Motorola MPC7450 class */            \
-       __PMC_CLASS(PPC970)     /* IBM PowerPC 970 class */             \
-       __PMC_CLASS(E500)       /* Freescale e500 class */              \
-       __PMC_CLASS(SOFT)       /* Software events */
+       __PMC_CLASS(TSC,        0x000,  "CPU Timestamp counter")        \
+       __PMC_CLASS(K7,         0x100,  "AMD K7 performance counters")  \
+       __PMC_CLASS(K8,         0x101,  "AMD K8 performance counters")  \
+       __PMC_CLASS(P5,         0x102,  "Intel Pentium counters")       \
+       __PMC_CLASS(P6,         0x103,  "Intel Pentium Pro counters")   \
+       __PMC_CLASS(P4,         0x104,  "Intel Pentium-IV counters")    \
+       __PMC_CLASS(IAF,        0x105,  "Intel Core2/Atom, fixed function") \
+       __PMC_CLASS(IAP,        0x106,  "Intel Core...Atom, programmable") \
+       __PMC_CLASS(UCF,        0x107,  "Intel Uncore fixed function")  \
+       __PMC_CLASS(UCP,        0x108,  "Intel Uncore programmable")    \
+       __PMC_CLASS(XSCALE,     0x200,  "Intel XScale counters")        \
+       __PMC_CLASS(ARMV7,      0x201,  "ARMv7")                        \
+       __PMC_CLASS(ARMV8,      0x202,  "ARMv8")                        \
+       __PMC_CLASS(MIPS24K,    0x300,  "MIPS 24K")                     \
+       __PMC_CLASS(OCTEON,     0x301,  "Cavium Octeon")                \
+       __PMC_CLASS(MIPS74K,    0x302,  "MIPS 74K")                     \
+       __PMC_CLASS(PPC7450,    0x400,  "Motorola MPC7450 class")       \
+       __PMC_CLASS(PPC970,     0x401,  "IBM PowerPC 970 class")        \
+       __PMC_CLASS(E500,       0x402,  "Freescale e500 class")         \
+       __PMC_CLASS(SOFT,       0x8000, "Software events")
 
 enum pmc_class {
 #undef  __PMC_CLASS
-#define        __PMC_CLASS(N)  PMC_CLASS_##N ,
+#define        __PMC_CLASS(S,V,D)      PMC_CLASS_##S = V,
        __PMC_CLASSES()
 };
 
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to