Skip to contentSkip to conversation

ELSA updated for kernel 2.6.8.1

PATCH In review Guillaume Thouvenin 2 participants 3 messages Aug 25, 2004 – Aug 26, 2004

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ELSA updated for kernel 2.6.8.1

Hello,

Here is the Enhanced Linux System Accounting patch for
kernel 2.6.8.1. This is the second announce because in the previous one we didn't attach the patch and we didn't explain what changes have been done. Sorry for the troubles.

This patch provides structures and functions to do accounting for a
group of process. There is two parts. On one hand there is two APIs that allow the creation and the destruction of an item called a "bank" that will contain a group of processes. There is also two other routines that can add or remove processes in a bank. On the other hand, you have the accounting mechanism that used bank to perform BSD-like accounting for a group of process. We used ioctl() interface to create/destroy a bank and also to add/remove a process in a bank. Currently, device driver is dev/elsacct and the major number is dynamically allocated, therefore you need to check the number in the /proc/devices and create the corresponding device.

The next step is to provide more accounting metrics. Therefore we are
working on the improvement of BSD accounting as ELSA accounting is based on it. We currently trying to get informations about IO as fields are already present in BSD-like accounting but they are not updated.

Changelog:
24/08/2004 - Guillaume
This patch is just a port for the kernel 2.6.8.1 so there are only minor changes that are relative to warning produce when applying the previous patch.

Chears,
Guillaume

--------------------------------------

drivers/elsacct/bank.c +434 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/drivers/elsacct/bank.c linux-2.6.8.1-elsa/drivers/elsacct/bank.c
@@ -0,0 +1,434 @@
+/*
+ *  driver/elsacct/bank.c
+ * 
+ *  ELSA - Enhanced Linux System Accounting
+ *  Guillaume Thouvenin - 26/04/2004
+ *
+ *  This file implements Enhanced Linux System Accounting. It 
+ *  provides structure and functions to manipulate "BANK". 
+ *
+ * 
+ *  This code is licenced under GPL.
+ */
+
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+
+#include <linux/bank.h>
+
+#include <asm/bug.h>
+
+ /*********************************************
+ * structures and macros used to manage banks *
+ *********************************************/
+
+#ifdef CONFIG_BANK_DEBUG
+#define dprintk(format...) printk(format)
+#else
+#define dprintk(format...)
+#endif
+
+#define ELSACCT_MIN_PID	1024	/* cannot add process if pid < ELSACCT_MIN_PID */
+
+/* Functions used internally */
+static int elsa_process_present(struct task_struct *p,
+				struct elsa_bank *b);
+
+ /********************************************************************
+ * Following functions can be use by a module (=> they are exported) *
+ ********************************************************************/
+
+/**
+ * elsa_bank_alloc - Allocates a new bank
+ *
+ * Description: allocates a new bank and returns the new created bank.
+ **/
+struct elsa_bank *elsa_bank_alloc(void)
+{
+	struct elsa_bank *b;
+
+	/* allocate space for the new bank */
+	b = (struct elsa_bank *) kmalloc(sizeof(struct elsa_bank),
+					 GFP_KERNEL);
+	if (!b) {
+		printk("elsa_bank_alloc: cannot allocate space\n");
+		return NULL;
+	}
+
+	/* Initialize fields */
+	b->info = NULL;
+	b->callback = NULL;
+	INIT_LIST_HEAD(&(b->data_head));
+
+	return b;
+}
+
+/**
+ * elsa_bank_free - Frees space occupied by a bank.
+ * @bank: a pointer to the bank to delete
+ * 
+ * Description: Free space used by bank
+ **/
+void elsa_bank_free(struct elsa_bank *b)
+{
+	/* 
+	 * Before releasing kernel memory occupied by the bank
+	 * we can do some action. We do it here instead of 
+	 * elsa_bank_remove() because elsa_bank_remove() is
+	 * called in a spinlock and the callback write records into
+	 * a file (and so cannot be done in a spinlock)
+	 */
+	if (b->callback) {
+		(b->callback) (ELSA_BANK_CALLBACK, b, NULL);
+	}
+
+	dprintk("elsa_bank_free: bank released\n");
+	kfree(b);
+}
+
+/**
+ * elsa_bank_add - Add a new bank to a bank's root
+ * @br: a pointer to bank's root
+ * @b: a pointer to a bank
+ * @fn: pointer to a callback called when bank or data are destroyed
+ * @info: pointer that can be used to store information 
+ * 
+ * Description: Add a new bank in the bank_root. It associates a 
+ * callback to a bank. The callback will be called each time a bank
+ * or a data will be removed. pointer @info can be used to store 
+ * usefull informations at bank level. 
+ **/
+int elsa_bank_add(struct bank_root *br,
+		  struct elsa_bank *b,
+		  int (*fn) (int, struct elsa_bank *, struct elsa_data *),
+		  void *info)
+{
+	/* 
+	 * give it an id. As it must be unique. As this function is only 
+	 * called from ioctl (drivers/erlsacct/elsacct.c) which is protected
+	 * by a spinlock, we know that only one instance will be execute. 
+	 * Currently we only used next_bid field but this mechnism 
+	 * can give problems if we use many banks and the bid go through
+	 * its maximum  
+	 */
+	b->bid = br->next_bid++;
+
+	if (b->bid <= 0) {
+		printk("elsa_bank_alloc: can not find bank identifier\n");
+		return 0;
+	}
+
+	/* Initialize fields */
+	b->info = info;
+	b->callback = fn;
+
+	/* 
+	 * add the new bank to the list of banks 
+	 */
+	list_add(&(b->bank_list), &(br->bank_head));
+
+	dprintk("elsa_bank_alloc: bank #%d created\n", b->bid);
+
+	return b->bid;
+}
+
+/**
+ * elsa_bank_remove - remove a bank from the list of banks 
+ * @b: a pointer to a bank
+ * 
+ * Description: remove a bank from a bank_root and returns the 
+ * identifier of the removed bank. When this function is called 
+ * bank _MUST_ be empty.
+ * Here are different steps of the operation:
+ *   1) Write accounting information
+ *   2) Remove it from the list of banks
+ **/
+int elsa_bank_remove(struct elsa_bank *b)
+{
+	if (unlikely(b == NULL) || unlikely(!list_empty(&b->data_head)))
+		BUG();
+
+	/* 
+	 * remove bank from the bank's list
+	 */
+	list_del(&(b->bank_list));
+
+	dprintk("elsa_bank_remove: bank #%d removed\n", b->bid);
+
+	return b->bid;
+}
+
+/**
+ * elsa_data_alloc - Allocates a new data
+ * 
+ * Description: allocates a new data and returns the new 
+ * created item. If an error is encountered NULL is returned.
+ * Here are different steps of the operation:
+ *   1) Allocate space for the new data
+ *   2) Initialize different fields
+ **/
+struct elsa_data *elsa_data_alloc(void)
+{
+	static struct elsa_data *d;
+
+	/* allocate space for the new data */
+	d = (struct elsa_data *) kmalloc(sizeof(struct elsa_data),
+					 GFP_KERNEL);
+	if (!d) {
+		printk("elsa_data_alloc: cannot allocate space\n");
+		return NULL;
+	}
+
+	/* Initialize fields */
+	d->bid = 0;
+	d->process = NULL;
+	INIT_LIST_HEAD(&(d->data_list));
+	INIT_LIST_HEAD(&(d->bank_list));
+
+	return d;
+}
+
+/**
+ * elsa_data_free - Free data
+ * @d: pointer to data to be removed
+ * 
+ * Description: releases memory used by data. 
+ **/
+void elsa_data_free(struct elsa_data *d)
+{
+	dprintk("elsa_data_free: data released\n");
+	kfree(d);
+}
+
+/**
+ * elsa_data_add - Add a process to a given bank
+ * @b  : pointer to a bank 
+ * @p  : pointer to a process
+ *
+ * Description: add a data to a bank by setting lists. Other fields are unchanged
+ * If an error is encountered, a negative value is returned. It can not return 0.
+ * Here are steps to perform:
+ *   1) Check arguments
+ *   2) Check if process is already in the bank
+ *   3) Initialize its fields
+ *   4) Return BID
+ **/
+int elsa_data_add(struct elsa_bank *b, struct elsa_data *d)
+{
+	static int present;
+
+	if (!b || !d || !(d->process)) {
+		dprintk("elsa_data_add: Wrong parameters\n");
+		return -EINVAL;
+	}
+
+	/* Bank ID cannot be equal to 0 */
+	if (unlikely(b->bid == 0))
+		BUG();
+
+	if (d->process->pid < ELSACCT_MIN_PID) {
+		dprintk("elsa_data_add: PID < %d\n", ELSACCT_MIN_PID);
+		return -EINVAL;
+	}
+
+	/* check if process is already present */
+	present = elsa_process_present(d->process, b);
+	if (present) {
+		/* Process already in the bank, so we can not add it */
+		return -EPERM;
+	}
+
+	/* Set BID */
+	d->bid = b->bid;
+
+	/* Add data in the list of bank's data */
+	list_add(&(d->data_list), &(b->data_head));
+
+	/* 
+	 * Add data in the list of process's bank. We modify task struct so we 
+	 * need to protect that.
+	 */
+	write_lock_irq(&tasklist_lock);
+	list_add(&(d->bank_list), &(d->process->bank_head));
+	write_unlock_irq(&tasklist_lock);
+
+	dprintk("elsa_data_add: Add process #%d to bank #%d\n",
+		d->process->pid, b->bid);
+
+	return b->bid;
+}
+
+/**
+ * elsa_data_remove - Remove a process to a given bank
+ * @b: pointer to the bank
+ * @d: pointer to the process
+ *
+ * Description: removes a given process from a given bank. 
+ * Return 0 if succedeed, BID of the bank if it is empty and 
+ * negative value otherwise
+ **/
+int elsa_data_remove(struct elsa_bank *b, struct elsa_data *d)
+{
+
+	if (!d)
+		return -EINVAL;
+
+	if (unlikely(d->process->pid < ELSACCT_MIN_PID))
+		BUG();
+
+	/* callback, we are in the child so callback code */
+	if (b && b->callback) {
+		(b->callback) (ELSA_DATA_CALLBACK, b, d);
+	}
+
+	/* remove data from the list of data */
+	list_del(&d->data_list);
+
+	/* Need access to head_list in task_struct */
+	/* We are in a spin_lock */
+	write_lock_irq(&tasklist_lock);
+	list_del(&d->bank_list);
+	write_unlock_irq(&tasklist_lock);
+
+	if (list_empty(&(b->data_head)))
+		/* bank is empty so we return its BID */
+		return b->bid;
+
+	return 0;		/* sucess and bank isn't empty */
+}
+
+/**
+ * elsa_get_bank - Returns a pointer to a bank
+ * @br: pointer to the root of banks
+ * @bid: the identifier of a bank
+ * 
+ * Description: finds the bank with given ID in the list of banks
+ **/
+struct elsa_bank *elsa_get_bank(struct bank_root *br, __u32 bid)
+{
+	static struct list_head *entry;
+	static struct elsa_bank *b;
+
+	if (bid == 0)
+		return NULL;
+
+	/*
+	 * We traverse the list searching for entries without changing
+	 * the list itself, so we use read_lock_irqsave
+	 */
+	list_for_each(entry, &(br->bank_head)) {
+		b = list_entry(entry, struct elsa_bank, bank_list);
+		if (b->bid == bid) {
+			return b;
+		}
+	}
+
+	/* We don't find the bank with corresponding BID */
+	return NULL;
+}
+
+/**
+ * elsa_get_data - Returns a pointer to an &elsa_data
+ * @pid: The identifier of the process
+ * @bid: The identifier of a bank
+ * 
+ * Description: returns a pointer to the container of a given pid in a 
+ * given bank. If the data isn't found, NULL is returned.
+ **/
+struct elsa_data *elsa_get_data(pid_t pid, __u32 bid)
+{
+	static struct list_head *entry;	/* entry == NULL */
+	static struct task_struct *p;	/* p == NULL */
+	static struct elsa_data *d;	/* d == NULL */
+
+	read_lock(&tasklist_lock);
+	p = find_task_by_pid(pid);
+	read_unlock(&tasklist_lock);
+
+	if ((p == NULL) || (bid == 0))
+		return NULL;
+
+	list_for_each(entry, &(p->bank_head)) {
+		d = list_entry(entry, struct elsa_data, bank_list);
+		/*  bank can not be empty */
+		if (unlikely(d == NULL))
+			BUG();
+
+		if (d->bid == bid) {
+			/* Got it */
+			return d;
+		}
+	}
+
+	return NULL;
+}
+
+/**
+ * elsa_get_first - Returns a pointer to the first element of a list
+ * @head: a pointer to the head of a list
+ * 
+ * Description: returns a pointer to the first data in a list.
+ * If list is empty, NULL is returned.
+ **/
+//struct elsa_data *elsa_get_first_data(struct list_head *head)
+//{
+//      static struct list_head *entry; /* entry == NULL */
+//      static struct elsa_data *d;     /* data == NULL */
+//
+//      list_for_each(entry, head) {
+//              d = list_entry(entry, struct elsa_data, data_list);
+//              return d;
+//      }
+//
+//      return NULL;
+//}
+
+/* memory management */
+EXPORT_SYMBOL(elsa_bank_alloc);
+EXPORT_SYMBOL(elsa_bank_free);
+EXPORT_SYMBOL(elsa_data_alloc);
+EXPORT_SYMBOL(elsa_data_free);
+
+/* data manipulation */
+EXPORT_SYMBOL(elsa_bank_add);
+EXPORT_SYMBOL(elsa_bank_remove);
+EXPORT_SYMBOL(elsa_data_add);
+EXPORT_SYMBOL(elsa_data_remove);
+
+/* get informations */
+EXPORT_SYMBOL(elsa_get_bank);
+EXPORT_SYMBOL(elsa_get_data);
+
+ /*************************************************
+ * Following functions are only used in this file *
+ *************************************************/
+
+/**
+ * elsa_process_present - Returns 1 if process is in the bank
+ * @p: the process identifier to look for
+ * @b: the bank in which we must look
+ * 
+ * Description: returns 1 if process is present in a bank, 0 otherwise
+ **/
+static int elsa_process_present(struct task_struct *p, struct elsa_bank *b)
+{
+	static struct list_head *entry;
+	static struct elsa_data *d;
+
+	read_lock(&tasklist_lock);
+	list_for_each(entry, &(b->data_head)) {
+		d = list_entry(entry, struct elsa_data, data_list);
+		if (d->process == p) {
+			/* Found it */
+			read_unlock(&tasklist_lock);
+			return 1;
+		}
+	}
+	read_unlock(&tasklist_lock);
+
+	/* Process not found in b */
+	return 0;
+}
drivers/elsacct/elsacct.c +827 -0

827 lines not on this page · raw message

drivers/elsacct/Kconfig +61 -0

61 lines not on this page · raw message

drivers/elsacct/Makefile +5 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/drivers/elsacct/Makefile linux-2.6.8.1-elsa/drivers/elsacct/Makefile
@@ -0,0 +1,5 @@
+#
+# Makefile for the Enhanced Linux System Accounting
+#
+obj-$(CONFIG_BANK)	+= bank.o
+obj-$(CONFIG_ELSACCT)	+= elsacct.o
drivers/Kconfig +2 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/drivers/Kconfig linux-2.6.8.1-elsa/drivers/Kconfig
@@ -4,6 +4,8 @@ menu "Device Drivers"
 
 source "drivers/base/Kconfig"
 
+source "drivers/elsacct/Kconfig"
+
 source "drivers/mtd/Kconfig"
 
 source "drivers/parport/Kconfig"
drivers/Makefile +1 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/drivers/Makefile linux-2.6.8.1-elsa/drivers/Makefile
@@ -51,3 +51,4 @@ obj-$(CONFIG_MCA)		+= mca/
 obj-$(CONFIG_EISA)		+= eisa/
 obj-$(CONFIG_CPU_FREQ)		+= cpufreq/
 obj-y				+= firmware/
+obj-y				+= elsacct/
include/linux/bank.h +183 -0

183 lines not on this page · raw message

include/linux/elsacct.h +85 -0

85 lines not on this page · raw message

include/linux/init_task.h +1 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/include/linux/init_task.h linux-2.6.8.1-elsa/include/linux/init_task.h
@@ -112,6 +112,7 @@ extern struct group_info init_groups;
 	.proc_lock	= SPIN_LOCK_UNLOCKED,				\
 	.switch_lock	= SPIN_LOCK_UNLOCKED,				\
 	.journal_info	= NULL,						\
+	.bank_head	= LIST_HEAD_INIT(tsk.bank_head),		\
 }
 
 
include/linux/sched.h +3 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/include/linux/sched.h linux-2.6.8.1-elsa/include/linux/sched.h
@@ -527,6 +527,9 @@ struct task_struct {
   	struct mempolicy *mempolicy;
   	short il_next;		/* could be shared with used_math */
 #endif
+
+/* List of BANK to which the process belong -- Used by ELSA */
+	struct list_head bank_head;
 };
 
 static inline pid_t process_group(struct task_struct *tsk)
kernel/exit.c +2 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/kernel/exit.c linux-2.6.8.1-elsa/kernel/exit.c
@@ -23,6 +23,7 @@
 #include <linux/mount.h>
 #include <linux/proc_fs.h>
 #include <linux/mempolicy.h>
+#include <linux/elsacct.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -59,6 +60,7 @@ repeat: 
 	BUG_ON(p->state < TASK_ZOMBIE);
  
 	atomic_dec(&p->user->processes);
+	elsacct_process_remove(p);
 	spin_lock(&p->proc_lock);
 	proc_dentry = proc_pid_unhash(p);
 	write_lock_irq(&tasklist_lock);
kernel/fork.c +14 -0
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/kernel/fork.c linux-2.6.8.1-elsa/kernel/fork.c
@@ -36,6 +36,7 @@
 #include <linux/mount.h>
 #include <linux/audit.h>
 #include <linux/rmap.h>
+#include <linux/elsacct.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -1030,6 +1031,19 @@ struct task_struct *copy_process(unsigne
 	INIT_LIST_HEAD(&p->ptrace_children);
 	INIT_LIST_HEAD(&p->ptrace_list);
 
+        /*
+         * Child is in the same BANK as parent. So we copy the list of banks
+         * from current to p. We can not just counting reference on the structure
+         * because the may evoluate differently. I think it's easier to
+         * do like this but maybe I'm wrong... -- guillaume
+         *
+         * We put that call outside lock_irq because elsa_process_copy()
+         * uses kmalloc(GFP_KERNEL). Therefore, as we modify task_struct,
+         * we manage lock_irq ourselves. -- guillaume
+         *
+         */
+        elsacct_process_copy(current, p);
+
 	/* Need tasklist lock for parent etc handling! */
 	write_lock_irq(&tasklist_lock);
 	/*
Makefile +1 -1
diff -uprN -X dontdiff linux-2.6.8.1-vanilla/Makefile linux-2.6.8.1-elsa/Makefile
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 6
 SUBLEVEL = 8
-EXTRAVERSION = .1
+EXTRAVERSION = .1-elsa
 NAME=Zonked Quokka
 
 # *DOCUMENTATION*
#1 lore raw
Quoted 3 lines · Guillaume Thouvenin

Here is the Enhanced Linux System Accounting patch for
kernel 2.6.8.1.

This looks suspiciously similar to SGI's Comprehensive System Accounting work.

Please see (and respond to) the comments regarding that code which I sent to this mailing list a few hours ago.

#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 3 lines · Guillaume Thouvenin

Here is the Enhanced Linux System Accounting patch for
kernel 2.6.8.1.

This looks suspiciously similar to SGI's Comprehensive System Accounting work.

Please see (and respond to) the comments regarding that code which I sent to this mailing list a few hours ago.

You're right the goal is the same as SGI's Comprehensive System
Accounting. When I started this project there was _no_ activity from the PAGG and CSA projects (it was during march if I remember). I saw that possibility to do accounting not only for one process but for a group of processes was missing in Linux. That's why, I started ELSA.

I will respond to your comments regarding the two projects on lse-tech mailing list.

Regards,
Guillaume