public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Laurent Vivier <Laurent.Vivier@bull.net>
To: kvm-devel@lists.sourceforge.net
Cc: Ingo Molnar <mingo@elte.hu>,
	Rusty Russell <rusty@rustcorp.com.au>,
	virtualization <virtualization@lists.linux-foundation.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH/RFC 3/4]Introduce "account modifiers" mechanism
Date: Thu, 16 Aug 2007 17:58:07 +0200	[thread overview]
Message-ID: <46C4740F.2050701@bull.net> (raw)
In-Reply-To: <46C4725A.4070607@bull.net>

[-- Attachment #1: Type: text/plain, Size: 473 bytes --]

[PATCH 3/3] introduce "account modifiers" mechanism in the kernel allowing a
module to modify the collected accounting for a given task. This implementation
is based on the "preempt_notifier". "account_system_time()" and
"account_user_time()" can call functions registered by a module to modify the
cputime value.

Signed-off-by: Laurent Vivier <Laurent.Vivier@bull.net>
-- 
------------- Laurent.Vivier@bull.net  --------------
          "Software is hard" - Donald Knuth

[-- Attachment #2: accounting_modifiers --]
[-- Type: text/plain, Size: 4091 bytes --]

Index: kvm/include/linux/sched.h
===================================================================
--- kvm.orig/include/linux/sched.h	2007-08-16 15:23:27.000000000 +0200
+++ kvm/include/linux/sched.h	2007-08-16 15:23:41.000000000 +0200
@@ -955,6 +955,10 @@
 	/* list of struct preempt_notifier: */
 	struct hlist_head preempt_notifiers;
 #endif
+#ifdef CONFIG_ACCOUNT_MODIFIERS
+	/* list of struct account_modifiers: */
+	struct hlist_head account_modifiers;
+#endif
 
 	unsigned short ioprio;
 #ifdef CONFIG_BLK_DEV_IO_TRACE
@@ -1873,6 +1877,31 @@
 }
 #endif
 
+#ifdef CONFIG_ACCOUNT_MODIFIERS
+struct account_modifier;
+struct account_ops {
+	cputime_t (*user_time)(struct account_modifier *,
+				struct task_struct *, cputime_t );
+	cputime_t (*system_time)(struct account_modifier *,
+				struct task_struct *, int, cputime_t);
+};
+
+struct account_modifier {
+	struct hlist_node link;
+	struct account_ops *ops;
+};
+
+void account_modifier_register(struct account_modifier *modifier);
+void account_modifier_unregister(struct account_modifier *modifier);
+
+static inline void account_modifier_init(struct account_modifier *modifier,
+					struct account_ops *ops)
+{
+	INIT_HLIST_NODE(&modifier->link);
+	modifier->ops = ops;
+}
+#endif
+
 #endif /* __KERNEL__ */
 
 #endif
Index: kvm/kernel/sched.c
===================================================================
--- kvm.orig/kernel/sched.c	2007-08-16 15:15:33.000000000 +0200
+++ kvm/kernel/sched.c	2007-08-16 15:23:28.000000000 +0200
@@ -1593,6 +1593,9 @@
 #ifdef CONFIG_PREEMPT_NOTIFIERS
 	INIT_HLIST_HEAD(&p->preempt_notifiers);
 #endif
+#ifdef CONFIG_ACCOUNT_MODIFIERS
+	INIT_HLIST_HEAD(&p->account_modifiers);
+#endif
 
 	/*
 	 * We mark the process as running here, but have not actually
@@ -1731,6 +1734,62 @@
 }
 
 #endif
+#ifdef CONFIG_ACCOUNT_MODIFIERS
+
+void account_modifier_register(struct account_modifier *modifier)
+{
+	hlist_add_head(&modifier->link, &current->account_modifiers);
+}
+EXPORT_SYMBOL_GPL(account_modifier_register);
+
+void account_modifier_unregister(struct account_modifier *modifier)
+{
+	hlist_del(&modifier->link);
+}
+EXPORT_SYMBOL_GPL(account_modifier_unregister);
+
+static cputime_t fire_user_time_account_modifiers(struct task_struct *curr,
+						  cputime_t cputime)
+{
+	struct account_modifier *modifier;
+	struct hlist_node *node;
+
+	hlist_for_each_entry(modifier, node, &curr->account_modifiers, link)
+		if (modifier->ops->user_time)
+			cputime = modifier->ops->user_time(modifier,
+							curr, cputime);
+	return cputime;
+}
+
+static cputime_t fire_system_time_account_modifiers(struct task_struct *curr,
+						      int hardirq_offset,
+						      cputime_t cputime)
+{
+	struct account_modifier *modifier;
+	struct hlist_node *node;
+
+	hlist_for_each_entry(modifier, node, &curr->account_modifiers, link)
+		if (modifier->ops->system_time)
+			cputime = modifier->ops->system_time(modifier,
+						curr, hardirq_offset, cputime);
+	return cputime;
+}
+
+#else
+
+static inline cputime_t fire_user_time_account_modifiers(struct task_struct *curr, cputime_t cputime)
+{
+	return cputime;
+}
+
+static inline cputime_t fire_system_time_account_modifiers(struct task_struct *curr,
+						      int hardirq_offset,
+						      cputime_t cputime)
+{
+	return cputime;
+}
+
+#endif
 
 /**
  * prepare_task_switch - prepare to switch tasks
@@ -3223,6 +3282,8 @@
 	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
 	cputime64_t tmp;
 
+	cputime = fire_user_time_account_modifiers(p, cputime);
+
 	p->utime = cputime_add(p->utime, cputime);
 
 	/* Add user time to cpustat. */
@@ -3246,6 +3307,8 @@
 	struct rq *rq = this_rq();
 	cputime64_t tmp;
 
+	cputime = fire_system_time_account_modifiers(p,hardirq_offset, cputime);
+
 	p->stime = cputime_add(p->stime, cputime);
 
 	/* Add system time to cpustat. */
@@ -6522,6 +6585,9 @@
 #ifdef CONFIG_PREEMPT_NOTIFIERS
 	INIT_HLIST_HEAD(&init_task.preempt_notifiers);
 #endif
+#ifdef CONFIG_ACCOUNT_MODIFIERS
+	INIT_HLIST_HEAD(&init_task.account_modifiers);
+#endif
 
 #ifdef CONFIG_SMP
 	nr_cpu_ids = highest_cpu + 1;

  parent reply	other threads:[~2007-08-16 15:59 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <46C4719A.2060308@bull.net>
2007-08-16 15:57 ` [PATCH/RFC 1/4]Introduce a new field "guest" in cpustat Laurent Vivier
     [not found] ` <46C4720F.7030304@bull.net>
2007-08-16 15:57   ` [PATCH/RFC 2/4]Introduce a new field "guest" in task_struct Laurent Vivier
     [not found]   ` <46C4725A.4070607@bull.net>
2007-08-16 15:58     ` Laurent Vivier [this message]
2007-08-16 22:39       ` [PATCH/RFC 3/4]Introduce "account modifiers" mechanism Rusty Russell
2007-08-17  7:35         ` Laurent Vivier
2007-08-17  8:30           ` Rusty Russell
2007-08-17  9:16             ` Laurent Vivier
2007-08-17 11:51               ` [PATCH/RFC 3/4, second shot]Introduce "account_guest_time" Laurent Vivier
2007-08-17 11:54                 ` [PATCH/RFC 4/4, second shot]KVM uses "account_guest_time()" Laurent Vivier
2007-08-17 13:03                   ` [kvm-devel] " Avi Kivity
2007-08-17 13:16                     ` Laurent Vivier
2007-08-19  7:39                       ` Avi Kivity
2007-08-17 12:59                 ` [kvm-devel] [PATCH/RFC 3/4, second shot]Introduce "account_guest_time" Avi Kivity
2007-08-17 12:55               ` [kvm-devel] [PATCH/RFC 3/4]Introduce "account modifiers" mechanism Avi Kivity
2007-08-17 13:08                 ` Laurent Vivier
2007-08-17 13:32                   ` Christian Borntraeger
2007-08-19  7:41                   ` Avi Kivity
2007-08-17 14:12                 ` Laurent Vivier
2007-08-19  7:38                   ` Avi Kivity
2007-08-20  7:30                     ` Laurent Vivier
2007-08-20  7:55                       ` Avi Kivity
     [not found]     ` <46C472D2.7000702@bull.net>
2007-08-16 15:59       ` [PATCH/RFC 4/4]Modify KVM to use the "account modifiers" Laurent Vivier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46C4740F.2050701@bull.net \
    --to=laurent.vivier@bull.net \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=rusty@rustcorp.com.au \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox