2.6.22-rc3 perfmon2 : sampling format support
PATCH 5/22 2.6.22-rc3 perfmon2 : sampling format support
This files contains the sampling format support.
Perfmon2 supports an in-kernel sampling buffer for performance reasons. Yet to ensure maximum flexibility to applications, the formats is which infmration is recorded into the kernel buffer is not specified by the interface. Instead it is delegated to a kernel plug-in modules called sampling formats.
Each formats controls:
- what is recorded in the the sampling buffer
- how the information is recorded
- when to notify the application to extract the information
- how the buffer is exported to user level
- hoe the buffer is allocated
Each format is identified via a 128-bit UUID which can be requested when the context is created with pfm_create_context().
The interface comes with a simple default sampling format. It records information sequentially in the buffer. Each entry, called sample, is composed of a fixed size header and a variable size body where the values of PMDS can be recorded based upon the user's request.
Sampling formats can be dynamically registered with perfmon. The management of sampling formats is implemented in perfmon_fmt.c:
pfm_register_smpl_fmt(struct pfm_smpl_fmt *fmt):
- register a new sampling format
pfm_unregister_smpl_fmt(pfm_uuid_t uuid):
- unregister a sampling format
It is possible to list the available formats by looking at /sys/kernel/perfmon/formats.
perfmon/perfmon_fmt.c +218 -0
@@ -0,0 +1,218 @@ +/* +* perfmon_fmt.c: perfmon2 sampling buffer format management +* +* This file implements the perfmon2 interface which +* provides access to the hardware performance counters +* of the host processor. +* +* The initial version of perfmon.c was written by +* Ganesh Venkitachalam, IBM Corp. +* +* Then it was modified for perfmon-1.x by Stephane Eranian and +* David Mosberger, Hewlett Packard Co. +* +* Version Perfmon-2.x is a complete rewrite of perfmon-1.x +* by Stephane Eranian, Hewlett Packard Co. +* +* Copyright (c) 1999-2006 Hewlett-Packard Development Company, L.P. +* Contributed by Stephane Eranian <[email protected]> +* David Mosberger-Tang <[email protected]> +* +* More information about perfmon available at: +* http://perfmon2.sf.net +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of version 2 of the GNU General Public +* License as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +* 02111-1307 USA +*/ +#include <linux/module.h> +#include <linux/perfmon.h> + +static __cacheline_aligned_in_smp DEFINE_SPINLOCK (pfm_smpl_fmt_lock ); +static LIST_HEAD (pfm_smpl_fmt_list); + +static inline int fmt_is_mod (struct pfm_smpl_fmt *f) +{ +return !(f->fmt_flags &PFM_FMTFL_IS_BUILTIN ); +} + +static struct pfm_smpl_fmt *pfm_find_fmt (char *name) +{ +struct pfm_smpl_fmt * entry; + +list_for_each_entry (entry, &pfm_smpl_fmt_list, fmt_list) { +if (!strcmp (entry->fmt_name, name)) +return entry; + } +return NULL ; +} +/* +* find a buffer format based on its name +*/ +struct pfm_smpl_fmt *pfm_smpl_fmt_get (char *name) +{ +struct pfm_smpl_fmt * fmt; + +spin_lock (&pfm_smpl_fmt_lock); + + fmt =pfm_find_fmt (name); + +/* +* increase module refcount +*/ +if (fmt &&fmt_is_mod (fmt) && !try_module_get (fmt->owner)) + fmt =NULL ; + +spin_unlock (&pfm_smpl_fmt_lock); + +return fmt; +} + +void pfm_smpl_fmt_put (struct pfm_smpl_fmt *fmt) +{ +if (fmt ==NULL || !fmt_is_mod (fmt)) +return ; +BUG_ON (fmt->owner ==NULL ); + +spin_lock (&pfm_smpl_fmt_lock); +module_put (fmt->owner); +spin_unlock (&pfm_smpl_fmt_lock); +} + +int pfm_fmt_register (struct pfm_smpl_fmt *fmt) +{ +int ret =0 ; + +if (atomic_read (&perfmon_disabled)) { +PFM_INFO ("perfmon disabled, cannot add sampling format" ); +return -ENOSYS ; + } + +/* some sanity checks */ +if (fmt ==NULL ) { +PFM_INFO ("perfmon: NULL format for register" ); +return -EINVAL ; + } + +if (fmt->fmt_name ==NULL ) { +PFM_INFO ("perfmon: format has no name" ); +return -EINVAL ; + } + +if (fmt->fmt_qdepth >PFM_MSGS_COUNT ) { +PFM_INFO ("perfmon: format %s requires %u msg queue depth (max %d)" , + fmt->fmt_name, + fmt->fmt_qdepth, +PFM_MSGS_COUNT ); +return -EINVAL ; + } + +/* +* fmt is missing the initialization of .owner = THIS_MODULE +* this is only valid when format is compiled as a module +*/ +if (fmt->owner ==NULL &&fmt_is_mod (fmt)) { +PFM_INFO ("format %s has no module owner" , fmt->fmt_name); +return -EINVAL ; + } +/* +* we need at least a handler +*/ +if (fmt->fmt_handler ==NULL ) { +PFM_INFO ("format %s has no handler" , fmt->fmt_name); +return -EINVAL ; + } + +/* +* format argument size cannot be bigger than PAGE_SIZE +*/ +if (fmt->fmt_arg_size >PAGE_SIZE ) { +PFM_INFO ("format %s arguments too big" , fmt->fmt_name); +return -EINVAL ; + } + +spin_lock (&pfm_smpl_fmt_lock); + +/* +* because of sysfs, we cannot have two formats with the same name +*/ +if (pfm_find_fmt (fmt->fmt_name)) { +PFM_INFO ("format %s already registered" , fmt->fmt_name); + ret = -EBUSY ; + goto out; + } + + ret =pfm_sysfs_add_fmt (fmt); +if (ret) { +PFM_INFO ("sysfs cannot add format entry for %s" , fmt->fmt_name); + goto out; + } + +list_add (&fmt->fmt_list, &pfm_smpl_fmt_list); + +PFM_INFO ("added sampling format %s" , fmt->fmt_name); +out: +spin_unlock (&pfm_smpl_fmt_lock); + +return ret; +} +EXPORT_SYMBOL (pfm_fmt_register); + +int pfm_fmt_unregister (struct pfm_smpl_fmt *fmt) +{ +struct pfm_smpl_fmt *fmt2; +int ret =0 ; + +if (!fmt || !fmt->fmt_name) { +PFM_DBG ("invalid fmt" ); +return -EINVAL ; + } + +spin_lock (&pfm_smpl_fmt_lock); + + fmt2 =pfm_find_fmt (fmt->fmt_name); +if (!fmt) { +PFM_INFO ("unregister failed, format not registered" ); + ret = -EINVAL ; + goto out; + } +list_del_init (&fmt->fmt_list); + +pfm_sysfs_remove_fmt (fmt); + +PFM_INFO ("removed sampling format: %s" , fmt->fmt_name); + +out: +spin_unlock (&pfm_smpl_fmt_lock); +return ret; + +} +EXPORT_SYMBOL (pfm_fmt_unregister); + +/* +* we defer adding the builtin formats to /sys/kernel/perfmon/formats +* until after the pfm sysfs subsystem is initialized. This function +* is called from pfm_sysfs_init() +*/ +void pfm_sysfs_builtin_fmt_add (void ) +{ +struct pfm_smpl_fmt * entry; + +/* +* locking not needed, kernel not fully booted +* when called +*/ +list_for_each_entry (entry, &pfm_smpl_fmt_list, fmt_list) { +pfm_sysfs_add_fmt (entry); + } +}
include/linux/perfmon_fmt.h +88 -0
88 lines not on this page · raw message
Quoted 3 lines · Stephane Eranian
Each format is identified via a 128-bit UUID which can be requested when the context is created with
pfm_create_context().
Why does it need a uuid? Isn't a name enough?
There is no documentation how the locking in this file works.
-Andi
Andi,
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Stephane Eranian
Each format is identified via a 128-bit UUID which can be requested when the context is created with
pfm_create_context().Why does it need a uuid? Isn't a name enough?
Sorry about that, the description is old and does not correspond to the actual code. The UUID has been removed during the previous code review. It has been replaced by a simple string.
Quoted 2 lines
There is no documentation how the locking in this file works.
Which particular file?
Signature · 1 line
-Stephane