Skip to contentSkip to conversation

Export jbd stats through procfs

PATCH In review Mingming Cao 6 participants 11 messages Jul 1, 2007 – Dec 1, 2007

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

PATCH 1/1 Export jbd stats through procfs

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

[ This probably should be rewritten to use debugfs? -- Ted]

--

include/linux/jbd2.h +92 -0
Index: linux-2.6.22-rc4/include/linux/jbd2.h
===================================================================
@@ -408,6 +408,16 @@
 };
 
 
+/*
+ * Some stats for checkpoint phase
+ */
+struct transaction_chp_stats_s {
+	unsigned long		cs_chp_time;
+	unsigned long		cs_forced_to_close;
+	unsigned long		cs_written;
+	unsigned long		cs_dropped;
+};
+
 /* The transaction_t type is the guts of the journaling mechanism.  It
  * tracks a compound transaction through its various states:
  *
@@ -543,6 +553,21 @@
 	spinlock_t		t_handle_lock;
 
 	/*
+	 * Longest time some handle had to wait for running transaction
+	 */
+	unsigned long		t_max_wait;
+
+	/*
+	 * When transaction started
+	 */
+	unsigned long		t_start;
+
+	/*
+	 * Checkpointing stats [j_checkpoint_sem]
+	 */
+	struct transaction_chp_stats_s t_chp_stats;
+
+	/*
 	 * Number of outstanding updates running on this transaction
 	 * [t_handle_lock]
 	 */
@@ -573,6 +598,57 @@
 
 };
 
+struct transaction_run_stats_s {
+	unsigned long		rs_wait;
+	unsigned long		rs_running;
+	unsigned long		rs_locked;
+	unsigned long		rs_flushing;
+	unsigned long		rs_logging;
+
+	unsigned long		rs_handle_count;
+	unsigned long		rs_blocks;
+	unsigned long		rs_blocks_logged;
+};
+
+struct transaction_stats_s
+{
+	int 			ts_type;
+	unsigned long		ts_tid;
+	union {
+		struct transaction_run_stats_s run;
+		struct transaction_chp_stats_s chp;
+	} u;
+};
+
+#define JBD2_STATS_RUN		1
+#define JBD2_STATS_CHECKPOINT	2
+
+#define ts_wait			u.run.rs_wait
+#define ts_running		u.run.rs_running
+#define ts_locked		u.run.rs_locked
+#define ts_flushing		u.run.rs_flushing
+#define ts_logging		u.run.rs_logging
+#define ts_handle_count		u.run.rs_handle_count
+#define ts_blocks		u.run.rs_blocks
+#define ts_blocks_logged	u.run.rs_blocks_logged
+
+#define ts_chp_time		u.chp.cs_chp_time
+#define ts_forced_to_close	u.chp.cs_forced_to_close
+#define ts_written		u.chp.cs_written
+#define ts_dropped		u.chp.cs_dropped
+
+#define CURRENT_MSECS		(jiffies_to_msecs(jiffies))
+
+static inline unsigned int
+jbd2_time_diff(unsigned int start, unsigned int end)
+{
+	if (unlikely(start > end))
+		end = end + (~0UL - start);
+	else
+		end -= start;
+	return end;
+}
+
 /**
  * struct journal_s - The journal_s type is the concrete type associated with
  *     journal_t.
@@ -634,6 +710,12 @@
  * @j_wbufsize: maximum number of buffer_heads allowed in j_wbuf, the
  *	number that will fit in j_blocksize
  * @j_last_sync_writer: most recent pid which did a synchronous write
+ * @j_history: Buffer storing the transactions statistics history
+ * @j_history_max: Maximum number of transactions in the statistics history
+ * @j_history_cur: Current number of transactions in the statistics history
+ * @j_history_lock: Protect the transactions statistics history
+ * @j_proc_entry: procfs entry for the jbd statistics directory
+ * @j_stats: Overall statistics
  * @j_private: An opaque pointer to fs-private information.
  */
 
@@ -826,6 +908,16 @@
 	pid_t			j_last_sync_writer;
 
 	/*
+	 * Journal statistics
+	 */
+	struct transaction_stats_s *j_history;
+	int			j_history_max;
+	int			j_history_cur;
+	spinlock_t		j_history_lock;
+	struct proc_dir_entry	*j_proc_entry;
+	struct transaction_stats_s j_stats;
+
+	/*
 	 * An opaque pointer to fs-private information.  ext3 puts its
 	 * superblock pointer here
 	 */
fs/jbd2/transaction.c +9 -0
Index: linux-2.6.22-rc4/fs/jbd2/transaction.c
===================================================================
@@ -59,6 +59,8 @@
 
 	J_ASSERT(journal->j_running_transaction == NULL);
 	journal->j_running_transaction = transaction;
+	transaction->t_max_wait = 0;
+	transaction->t_start = CURRENT_MSECS;
 
 	return transaction;
 }
@@ -85,6 +87,7 @@
 	int nblocks = handle->h_buffer_credits;
 	transaction_t *new_transaction = NULL;
 	int ret = 0;
+	unsigned long ts = CURRENT_MSECS;
 
 	if (nblocks > journal->j_max_transaction_buffers) {
 		printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
@@ -218,6 +221,12 @@
 	/* OK, account for the buffers that this operation expects to
 	 * use and add the handle to the running transaction. */
 
+	if (time_after(transaction->t_start, ts)) {
+		ts = jbd2_time_diff(ts, transaction->t_start);
+		if (ts > transaction->t_max_wait)
+			transaction->t_max_wait = ts;
+	}
+
 	handle->h_transaction = transaction;
 	transaction->t_outstanding_credits += nblocks;
 	transaction->t_updates++;
fs/jbd2/checkpoint.c +31 -2
Index: linux-2.6.22-rc4/fs/jbd2/checkpoint.c
===================================================================
@@ -232,7 +232,8 @@
  * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  */
 static int __process_buffer(journal_t *journal, struct journal_head *jh,
-			struct buffer_head **bhs, int *batch_count)
+			struct buffer_head **bhs, int *batch_count,
+			transaction_t *transaction)
 {
 	struct buffer_head *bh = jh2bh(jh);
 	int ret = 0;
@@ -250,6 +251,7 @@
 		transaction_t *t = jh->b_transaction;
 		tid_t tid = t->t_tid;
 
+		transaction->t_chp_stats.cs_forced_to_close++;
 		spin_unlock(&journal->j_list_lock);
 		jbd_unlock_bh_state(bh);
 		jbd2_log_start_commit(journal, tid);
@@ -279,6 +281,7 @@
 		bhs[*batch_count] = bh;
 		__buffer_relink_io(jh);
 		jbd_unlock_bh_state(bh);
+		transaction->t_chp_stats.cs_written++;
 		(*batch_count)++;
 		if (*batch_count == NR_BATCH) {
 			spin_unlock(&journal->j_list_lock);
@@ -322,6 +325,8 @@
 	if (!journal->j_checkpoint_transactions)
 		goto out;
 	transaction = journal->j_checkpoint_transactions;
+	if (transaction->t_chp_stats.cs_chp_time == 0)
+		transaction->t_chp_stats.cs_chp_time = CURRENT_MSECS;
 	this_tid = transaction->t_tid;
 restart:
 	/*
@@ -346,7 +351,8 @@
 				retry = 1;
 				break;
 			}
-			retry = __process_buffer(journal, jh, bhs,&batch_count);
+			retry = __process_buffer(journal, jh, bhs, &batch_count,
+						 transaction);
 			if (!retry && lock_need_resched(&journal->j_list_lock)){
 				spin_unlock(&journal->j_list_lock);
 				retry = 1;
@@ -668,6 +674,8 @@
 
 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
 {
+	struct transaction_stats_s stats;
+
 	assert_spin_locked(&journal->j_list_lock);
 	if (transaction->t_cpnext) {
 		transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
@@ -693,5 +701,26 @@
 	J_ASSERT(journal->j_running_transaction != transaction);
 
 	jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
+
+	/*
+	 * File the transaction for history
+	 */
+	if (transaction->t_chp_stats.cs_written != 0 ||
+			transaction->t_chp_stats.cs_chp_time != 0) {
+		stats.ts_type = JBD2_STATS_CHECKPOINT;
+		stats.ts_tid = transaction->t_tid;
+		stats.u.chp = transaction->t_chp_stats;
+		if (stats.ts_chp_time)
+			stats.ts_chp_time =
+				jbd2_time_diff(stats.ts_chp_time,
+						CURRENT_MSECS);
+		spin_lock(&journal->j_history_lock);
+		memcpy(journal->j_history + journal->j_history_cur, &stats,
+				sizeof(stats));
+		if (++journal->j_history_cur == journal->j_history_max)
+			journal->j_history_cur = 0;
+		spin_unlock(&journal->j_history_lock);
+	}
+
 	kfree(transaction);
 }
fs/jbd2/commit.c +47 -0

95 lines not on this page · raw message

fs/jbd2/journal.c +332 -0

386 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · On Sun, 01 Jul 2007 03:38:10 -0400 Mingming Cao <[email protected]> wrote:

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

[ This probably should be rewritten to use debugfs? -- Ted]

Was a decision ever made on whether this should remain on procfs or be move to use debugfs? I can recall this being discuss but don't recall a firm decision on it.

-JRS

#5 lore raw
In reply to Andrew Morton
Quoted 20 lines · Andrew Morton
Quoted 10 lines · On Tue, 10 Jul 2007 23:21:49 -0400 "Cédric Augonnet"…

2007/7/10, Andrew Morton <[email protected]>:

Hi all,

Quoted 6 lines
Quoted 5 lines
fs/jbd2/journal.c +5 -0
+     size = sizeof(struct transaction_stats_s);
+     s->stats = kmalloc(size, GFP_KERNEL);
+     if (s == NULL) {
+             kfree(s);
+             return -EIO;
Quoted 7 lines
Quoted 1 line

ENOMEM

I'm sorry if i missed some point, but i just don't see the use of such a kfree here, not sure Andrew meant you should only return ENOMEM instead, but why issuing those kfree(NULL), instead of just a if (!s) return ENOMEM ?

Quoted 5 lines

You found a bug. It was meant to be

	if (s->stats == NULL)

Thanks, I will make sure this get fixed in ext4 patch queue.

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to Mingming Cao
Quoted 9 lines · On Sun, 01 Jul 2007 03:38:10 -0400 Mingming Cao <[email protected]> wrote:

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

[ This probably should be rewritten to use debugfs? -- Ted]

I suppose that given that we're creating a spot in debugfs for the jbd2 debug code, yes, this also should be moved over.

But the jbd2 debug debugfs entries were kernel-wide whereas this is per-superblock. I think.

That email from Alex contains pretty important information. I suggest that it be added to the changelog after accuracy-checking. Addition to Documentation/filesystems/ext4.txt would be good.

Quoted 9 lines

--

Index: linux-2.6.22-rc4/include/linux/jbd2.h
===================================================================

include/linux/jbd2.h +0 -0
@@ -408,6 +408,16 @@
 };
 

include/linux/jbd2.h line 419

Quoted 10 lines
include/linux/jbd2.h +9 -0
 
+/*
+ * Some stats for checkpoint phase
+ */
+struct transaction_chp_stats_s {
+	unsigned long		cs_chp_time;
+	unsigned long		cs_forced_to_close;
+	unsigned long		cs_written;
+	unsigned long		cs_dropped;
+};

It would be nice to document what units all these fields are in. Jiffies, I assume.

Quoted 57 lines
include/linux/jbd2.h +43 -0
 /* The transaction_t type is the guts of the journaling mechanism.  It
  * tracks a compound transaction through its various states:
  *
@@ -543,6 +553,21 @@
 	spinlock_t		t_handle_lock;
 
 	/*
+	 * Longest time some handle had to wait for running transaction
+	 */
+	unsigned long		t_max_wait;
+
+	/*
+	 * When transaction started
+	 */
+	unsigned long		t_start;
+
+	/*
+	 * Checkpointing stats [j_checkpoint_sem]
+	 */
+	struct transaction_chp_stats_s t_chp_stats;
+
+	/*
 	 * Number of outstanding updates running on this transaction
 	 * [t_handle_lock]
 	 */
@@ -573,6 +598,57 @@
 
 };
 
+struct transaction_run_stats_s {
+	unsigned long		rs_wait;
+	unsigned long		rs_running;
+	unsigned long		rs_locked;
+	unsigned long		rs_flushing;
+	unsigned long		rs_logging;
+
+	unsigned long		rs_handle_count;
+	unsigned long		rs_blocks;
+	unsigned long		rs_blocks_logged;
+};
+
+struct transaction_stats_s
+{
+	int 			ts_type;
+	unsigned long		ts_tid;
+	union {
+		struct transaction_run_stats_s run;
+		struct transaction_chp_stats_s chp;
+	} u;
+};
+
+#define JBD2_STATS_RUN		1
+#define JBD2_STATS_CHECKPOINT	2
+
+#define ts_wait			u.run.rs_wait
+#define ts_running		u.run.rs_running
+#define ts_locked		u.run.rs_locked

include/linux/jbd2.h line 638

Quoted 10 lines
include/linux/jbd2.h +10 -0
+#define ts_flushing		u.run.rs_flushing
+#define ts_logging		u.run.rs_logging
+#define ts_handle_count		u.run.rs_handle_count
+#define ts_blocks		u.run.rs_blocks
+#define ts_blocks_logged	u.run.rs_blocks_logged
+
+#define ts_chp_time		u.chp.cs_chp_time
+#define ts_forced_to_close	u.chp.cs_forced_to_close
+#define ts_written		u.chp.cs_written
+#define ts_dropped		u.chp.cs_dropped

That's a bit sleazy. We can drop the "u" from 'struct transaction_stats_s' and make it an anonymous union, then open-code foo.run.rs_wait everywhere.

But that's a bit sleazy too, because the reader of the code would not know that a write to foo.run.rs_wait will stomp on the value of foo.chp.cs_chp_time.

So to minimize reader confusion it would be best I think to just open-code the full u.run.rs_wait at all code-sites.

The macros above are the worst possible choice: they hide information from the code-reader just to save the code-writer a bit of typing. But we very much want to optimise for code-readers, not for code-writers.

include/linux/jbd2.h line 640

Quoted 1 line
include/linux/jbd2.h +1 -0
+#define CURRENT_MSECS		(jiffies_to_msecs(jiffies))

hm, that isn't something which should be in an ext4 header file. And it shouldn't be in UPPER CASE and it shouldn't pretend to be a constant (or a global scalar).

IOW: yuk.

How's about raising a separate, standalone patch which creates a new kernel-wide coded-in-C function such as

unsigned long jiffies_in_msecs(void);

? (That's assuming we don't already have one. Most likely we have seven of them hiding in various dark corners).

include/linux/jbd2.h line 650

Quoted 9 lines
include/linux/jbd2.h +9 -0
+static inline unsigned int
+jbd2_time_diff(unsigned int start, unsigned int end)
+{
+	if (unlikely(start > end))
+		end = end + (~0UL - start);
+	else
+		end -= start;
+	return end;
+}

I don't know what this does and I am disinclined to reverse-engineer its uncommentedness. But it looks like something which should be in some kernel-wide header file?

Quoted 18 lines
include/linux/jbd2.h +6 -0
 /**
  * struct journal_s - The journal_s type is the concrete type associated with
  *     journal_t.
@@ -634,6 +710,12 @@
  * @j_wbufsize: maximum number of buffer_heads allowed in j_wbuf, the
  *	number that will fit in j_blocksize
  * @j_last_sync_writer: most recent pid which did a synchronous write
+ * @j_history: Buffer storing the transactions statistics history
+ * @j_history_max: Maximum number of transactions in the statistics history
+ * @j_history_cur: Current number of transactions in the statistics history
+ * @j_history_lock: Protect the transactions statistics history
+ * @j_proc_entry: procfs entry for the jbd statistics directory
+ * @j_stats: Overall statistics
  * @j_private: An opaque pointer to fs-private information.
  */
 
@@ -826,6 +908,16 @@
 	pid_t			j_last_sync_writer;

include/linux/jbd2.h line 918

Quoted 10 lines
include/linux/jbd2.h +8 -0
 
 	/*
+	 * Journal statistics
+	 */
+	struct transaction_stats_s *j_history;
+	int			j_history_max;
+	int			j_history_cur;
+	spinlock_t		j_history_lock;
+	struct proc_dir_entry	*j_proc_entry;
+	struct transaction_stats_s j_stats;

hm, we seem to be documenting the struct journal_s fields at two different places, possibly with duplication.

Documenting the fields at the top in kernedoc-style drives me wild, frankly. We really really do want to be able to see the description of each field (which is vital information for understanding the code!) right there at the definition site.

But at least it's documented somewhere.

Quoted 9 lines
include/linux/jbd2.h +1 -0
+	/*
 	 * An opaque pointer to fs-private information.  ext3 puts its
 	 * superblock pointer here
 	 */

Index: linux-2.6.22-rc4/fs/jbd2/transaction.c
===================================================================

fs/jbd2/transaction.c line 63

Quoted 5 lines
fs/jbd2/transaction.c +2 -0
@@ -59,6 +59,8 @@
 
 	J_ASSERT(journal->j_running_transaction == NULL);
 	journal->j_running_transaction = transaction;
+	transaction->t_max_wait = 0;
+	transaction->t_start = CURRENT_MSECS;

Ah, it's in milliseconds!

Good, albeit somewhat inefficient. Usually we'd accumulate jiffies and convert to/from milliseconds at the user interface boundary.

Quoted 29 lines
fs/jbd2/checkpoint.c +4 -1
@@ -232,7 +232,8 @@
  * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  */
 static int __process_buffer(journal_t *journal, struct journal_head *jh,
-			struct buffer_head **bhs, int *batch_count)
+			struct buffer_head **bhs, int *batch_count,
+			transaction_t *transaction)
 {
 	struct buffer_head *bh = jh2bh(jh);
 	int ret = 0;
@@ -250,6 +251,7 @@
 		transaction_t *t = jh->b_transaction;
 		tid_t tid = t->t_tid;
 
+		transaction->t_chp_stats.cs_forced_to_close++;
 		spin_unlock(&journal->j_list_lock);
 		jbd_unlock_bh_state(bh);
 		jbd2_log_start_commit(journal, tid);
@@ -279,6 +281,7 @@
 		bhs[*batch_count] = bh;
 		__buffer_relink_io(jh);
 		jbd_unlock_bh_state(bh);
+		transaction->t_chp_stats.cs_written++;
 		(*batch_count)++;
 		if (*batch_count == NR_BATCH) {
 			spin_unlock(&journal->j_list_lock);

fs/jbd2/checkpoint.c line 329

Quoted 5 lines
fs/jbd2/checkpoint.c +2 -0
@@ -322,6 +325,8 @@
 	if (!journal->j_checkpoint_transactions)
 		goto out;
 	transaction = journal->j_checkpoint_transactions;
+	if (transaction->t_chp_stats.cs_chp_time == 0)
+		transaction->t_chp_stats.cs_chp_time = CURRENT_MSECS;

Oh dear, I have no clue what cs_chp_time represents. Number of milliseconds for this transaction, I guess. In which case we must be either a) triggering stats collection via some user interface of b) averaging these on an ongoing basis or c) something else.

Let me read on...

Quoted 37 lines
fs/jbd2/checkpoint.c +15 -1
 	this_tid = transaction->t_tid;
 restart:
 	/*
@@ -346,7 +351,8 @@
 				retry = 1;
 				break;
 			}
-			retry = __process_buffer(journal, jh, bhs,&batch_count);
+			retry = __process_buffer(journal, jh, bhs, &batch_count,
+						 transaction);
 			if (!retry && lock_need_resched(&journal->j_list_lock)){
 				spin_unlock(&journal->j_list_lock);
 				retry = 1;
@@ -668,6 +674,8 @@
 
 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
 {
+	struct transaction_stats_s stats;
+
 	assert_spin_locked(&journal->j_list_lock);
 	if (transaction->t_cpnext) {
 		transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
@@ -693,5 +701,26 @@
 	J_ASSERT(journal->j_running_transaction != transaction);
 
 	jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
+
+	/*
+	 * File the transaction for history
+	 */
+	if (transaction->t_chp_stats.cs_written != 0 ||
+			transaction->t_chp_stats.cs_chp_time != 0) {
+		stats.ts_type = JBD2_STATS_CHECKPOINT;
+		stats.ts_tid = transaction->t_tid;
+		stats.u.chp = transaction->t_chp_stats;
+		if (stats.ts_chp_time)
+			stats.ts_chp_time =

fs/jbd2/checkpoint.c line 724

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+				jbd2_time_diff(stats.ts_chp_time,
+						CURRENT_MSECS);
+		spin_lock(&journal->j_history_lock);
+		memcpy(journal->j_history + journal->j_history_cur, &stats,
+				sizeof(stats));
+		if (++journal->j_history_cur == journal->j_history_max)
+			journal->j_history_cur = 0;
+		spin_unlock(&journal->j_history_lock);
+	}
+

Ah, it's a per-journal ringbuffer. Fair enough.

Quoted 97 lines
fs/jbd2/checkpoint.c +97 -0
+struct jbd2_stats_proc_session {
+	journal_t *journal;
+	struct transaction_stats_s *stats;
+	int start;
+	int max;
+};
+
+static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
+					struct transaction_stats_s *ts,
+					int first)
+{
+	if (ts == s->stats + s->max)
+		ts = s->stats;
+	if (!first && ts == s->stats + s->start)
+		return NULL;
+	while (ts->ts_type == 0) {
+		ts++;
+		if (ts == s->stats + s->max)
+			ts = s->stats;
+		if (ts == s->stats + s->start)
+			return NULL;
+	}
+	return ts;
+
+}
+
+static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
+{
+	struct jbd2_stats_proc_session *s = seq->private;
+	struct transaction_stats_s *ts;
+	int l = *pos;
+
+	if (l == 0)
+		return SEQ_START_TOKEN;
+	ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
+	if (!ts)
+		return NULL;
+	l--;
+	while (l) {
+		ts = jbd2_history_skip_empty(s, ++ts, 0);
+		if (!ts)
+			break;
+		l--;
+	}
+	return ts;
+}
+
+static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	struct jbd2_stats_proc_session *s = seq->private;
+	struct transaction_stats_s *ts = v;
+
+	++*pos;
+	if (v == SEQ_START_TOKEN)
+		return jbd2_history_skip_empty(s, s->stats + s->start, 1);
+	else
+		return jbd2_history_skip_empty(s, ++ts, 0);
+}
+
+static int jbd2_seq_history_show(struct seq_file *seq, void *v)
+{
+	struct transaction_stats_s *ts = v;
+	if (v == SEQ_START_TOKEN) {
+		seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
+				"%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
+				"wait", "run", "lock", "flush", "log", "hndls",
+				"block", "inlog", "ctime", "write", "drop",
+				"close");
+		return 0;
+	}
+	if (ts->ts_type == JBD2_STATS_RUN)
+		seq_printf(seq, "%-4s %-5lu %-5lu %-5lu %-5lu %-5lu %-5lu "
+				"%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
+				ts->ts_wait, ts->ts_running, ts->ts_locked,
+				ts->ts_flushing, ts->ts_logging,
+				ts->ts_handle_count, ts->ts_blocks,
+				ts->ts_blocks_logged);
+	else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
+		seq_printf(seq, "%-4s %-5lu %48s %-5lu %-5lu %-5lu %-5lu\n",
+				"C", ts->ts_tid, " ", ts->ts_chp_time,
+				ts->ts_written, ts->ts_dropped,
+				ts->ts_forced_to_close);
+	else
+		J_ASSERT(0);
+	return 0;
+}
+
+static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
+{
+}
+
+static struct seq_operations jbd2_seq_history_ops = {
+	.start  = jbd2_seq_history_start,
+	.next   = jbd2_seq_history_next,
+	.stop   = jbd2_seq_history_stop,
+	.show   = jbd2_seq_history_show,
+};

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+
+static int jbd2_seq_history_open(struct inode *inode, struct file *file)
+{
+	journal_t *journal = PDE(inode)->data;
+	struct jbd2_stats_proc_session *s;
+	int rc, size;
+
+	s = kmalloc(sizeof(*s), GFP_KERNEL);
+	if (s == NULL)
+		return -EIO;

ENOMEM.

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	size = sizeof(struct transaction_stats_s) * journal->j_history_max;
+	s->stats = kmalloc(size, GFP_KERNEL);
+	if (s == NULL) {
+		kfree(s);
+		return -EIO;

ENOMEM.

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+	}
+	spin_lock(&journal->j_history_lock);
+	memcpy(s->stats, journal->j_history, size);
+	s->max = journal->j_history_max;
+	s->start = journal->j_history_cur % s->max;
+	spin_unlock(&journal->j_history_lock);
+
+	rc = seq_open(file, &jbd2_seq_history_ops);
+	if (rc == 0) {
+		struct seq_file *m = (struct seq_file *)file->private_data;

unneeded cast of void*

fs/jbd2/checkpoint.c

Quoted 12 lines
fs/jbd2/checkpoint.c +12 -0
+		m->private = s;
+	} else {
+		kfree(s->stats);
+		kfree(s);
+	}
+	return rc;
+
+}
+
+static int jbd2_seq_history_release(struct inode *inode, struct file *file)
+{
+	struct seq_file *seq = (struct seq_file *)file->private_data;

unneeded cast of void*

Quoted 8 lines
fs/jbd2/checkpoint.c +6 -0
+	struct jbd2_stats_proc_session *s = seq->private;
+	kfree(s->stats);
+	kfree(s);
+	return seq_release(inode, file);
+}
+
 
...

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +9 -0
 
+static int jbd2_seq_info_open(struct inode *inode, struct file *file)
+{
+	journal_t *journal = PDE(inode)->data;
+	struct jbd2_stats_proc_session *s;
+	int rc, size;
+
+	s = kmalloc(sizeof(*s), GFP_KERNEL);
+	if (s == NULL)
+		return -EIO;

ENOMEM

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	size = sizeof(struct transaction_stats_s);
+	s->stats = kmalloc(size, GFP_KERNEL);
+	if (s == NULL) {
+		kfree(s);
+		return -EIO;

ENOMEM

Quoted 11 lines
fs/jbd2/checkpoint.c +11 -0
+	}
+	spin_lock(&journal->j_history_lock);
+	memcpy(s->stats, &journal->j_stats, size);
+	s->journal = journal;
+	spin_unlock(&journal->j_history_lock);
+
+	rc = seq_open(file, &jbd2_seq_info_ops);
+	if (rc == 0) {
+		struct seq_file *m = (struct seq_file *)file->private_data;
+		m->private = s;
+	} else {

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+		kfree(s->stats);
+		kfree(s);
+	}
+	return rc;
+
+}
+
+static int jbd2_seq_info_release(struct inode *inode, struct file *file)
+{
+	struct seq_file *seq = (struct seq_file *)file->private_data;

Unneeded cast

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	struct jbd2_stats_proc_session *s = seq->private;
+	kfree(s->stats);
+	kfree(s);
+	return seq_release(inode, file);
+}

We would usually put a blanck line betwee end-of-locals and start-of-code.

fs/jbd2/checkpoint.c

Quoted 9 lines
fs/jbd2/checkpoint.c +9 -0
+static struct file_operations jbd2_seq_info_fops = {
+	.owner		= THIS_MODULE,
+	.open           = jbd2_seq_info_open,
+	.read           = seq_read,
+	.llseek         = seq_lseek,
+	.release        = jbd2_seq_info_release,
+};
+
+static struct proc_dir_entry *proc_jbd2_stats = NULL;

Unneeded (and wasteful) initialisation (checkpatch should catch this)

fs/jbd2/checkpoint.c

Quoted 3 lines
fs/jbd2/checkpoint.c +3 -0
+static void jbd2_stats_proc_init(journal_t *journal)
+{
+	char name[64];

This should have size BDEVNAME_SIZE.

Quoted 12 lines
fs/jbd2/checkpoint.c +12 -0
+	snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
+	journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
+	if (journal->j_proc_entry) {
+		struct proc_dir_entry *p;
+		p = create_proc_entry("history", S_IRUGO,
+				journal->j_proc_entry);
+		if (p) {
+			p->proc_fops = &jbd2_seq_history_fops;
+			p->data = journal;
+			p = create_proc_entry("info", S_IRUGO,
+						journal->j_proc_entry);
+			if (p) {

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+				p->proc_fops = &jbd2_seq_info_fops;
+				p->data = journal;
+			}
+		}
+	}
+}
+
+static void jbd2_stats_proc_exit(journal_t *journal)
+{
+	char name[64];

ditto

Quoted 13 lines
fs/jbd2/checkpoint.c +13 -0
+	snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
+	remove_proc_entry("info", journal->j_proc_entry);
+	remove_proc_entry("history", journal->j_proc_entry);
+	remove_proc_entry(name, proc_jbd2_stats);
+}
+
+static void journal_init_stats(journal_t *journal)
+{
+	int size;
+
+	if (proc_jbd2_stats == NULL)
+		return;
+

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+	journal->j_history_max = 100;
+	size = sizeof(struct transaction_stats_s) * journal->j_history_max;
+	journal->j_history = kmalloc(size, GFP_KERNEL);
+	if (journal->j_history == NULL) {
+		journal->j_history_max = 0;
+		return;
+	}
+	memset(journal->j_history, 0, size);
+	spin_lock_init(&journal->j_history_lock);
+}

So the "100" is not alterable at present although the code is pretty much set up to do this (code which alters the size of the history buffer will need to take j_history_lock).

fs/jbd2/checkpoint.c

Quoted 1 line
fs/jbd2/checkpoint.c +1 -0
+	journal_init_stats(journal);

This is all a heck of a lot of new code. Should we plan to make it Kconfigurable?

Quoted 1 line

+#if defined(CONFIG_PROC_FS)

#ifdef CONFIG_PROC_FS

would be more typical

Quoted 1 line

+#define JBD2_STATS_PROC_NAME "fs/jbd2"

hm, now what's going one here. Maybe I misiunderstood, and the stats are not per-superblock at all. That would be a shame.

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+static void __init jbd2_create_jbd_stats_proc_entry(void)
+{
+	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
+}
+

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+static void __exit jbd2_remove_jbd_stats_proc_entry(void)
+{
+	if (proc_jbd2_stats)
+		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
+}
+
+#else
+
+#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
+#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)

These could be coded in C. Remember: only use macros for things which _cannot_ be coded in C.

Quoted 20 lines
fs/jbd2/checkpoint.c +4 -0
+#endif
+
 struct kmem_cache *jbd2_handle_cache;
 
 static int __init journal_init_handle_cache(void)
@@ -2046,6 +2376,7 @@
 	if (ret != 0)
 		jbd2_journal_destroy_caches();
 	jbd2_create_debugfs_entry();
+	jbd2_create_jbd_stats_proc_entry();
 	return ret;
 }
 
@@ -2057,6 +2388,7 @@
 		printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
 #endif
 	jbd2_remove_debugfs_entry();
+	jbd2_remove_jbd_stats_proc_entry();
 	jbd2_journal_destroy_caches();
 }
#3 lore raw

2007/7/10, Andrew Morton <[email protected]>:

Hi all,

Quoted 7 lines

fs/jbd2/journal.c line 856

Quoted 5 lines
fs/jbd2/journal.c +5 -0
+     size = sizeof(struct transaction_stats_s);
+     s->stats = kmalloc(size, GFP_KERNEL);
+     if (s == NULL) {
+             kfree(s);
+             return -EIO;

ENOMEM

I'm sorry if i missed some point, but i just don't see the use of such a kfree here, not sure Andrew meant you should only return ENOMEM instead, but why issuing those kfree(NULL), instead of just a if (!s) return ENOMEM ?

Regards,
Cédric

#7 lore raw
Quoted 7 lines · Cédric Augonnet

2007/7/10, Andrew Morton <[email protected]>:

Hi all,

Quoted 3 lines
Quoted 3 lines
fs/jbd2/journal.c +3 -0
+     size = sizeof(struct transaction_stats_s);
+     s->stats = kmalloc(size, GFP_KERNEL);
+     if (s == NULL) {
	       ^
Quoted 4 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+             kfree(s);
+             return -EIO;

ENOMEM

That, and

	if (s->stats == NULL)
		kfree(s);
Quoted 4 lines

I'm sorry if i missed some point, but i just don't see the use of such a kfree here, not sure Andrew meant you should only return ENOMEM instead, but why issuing those kfree(NULL), instead of just a if (!s) return ENOMEM ?

kfree() is correct, check isn't.

#4 lore raw
In reply to Cédric Augonnet
Quoted 10 lines · On Tue, 10 Jul 2007 23:21:49 -0400 "Cédric Augonnet"…

2007/7/10, Andrew Morton <[email protected]>:

Hi all,

Quoted 6 lines
Quoted 5 lines
fs/jbd2/journal.c +5 -0
+     size = sizeof(struct transaction_stats_s);
+     s->stats = kmalloc(size, GFP_KERNEL);
+     if (s == NULL) {
+             kfree(s);
+             return -EIO;
Quoted 7 lines
Quoted 1 line

ENOMEM

I'm sorry if i missed some point, but i just don't see the use of such a kfree here, not sure Andrew meant you should only return ENOMEM instead, but why issuing those kfree(NULL), instead of just a if (!s) return ENOMEM ?

You found a bug. It was meant to be

	if (s->stats == NULL)

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
Quoted 7 lines · On Sun, 01 Jul 2007 03:38:10 -0400 Mingming Cao <[email protected]> wrote:

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

Quoted 9 lines
Quoted 2 lines

[ This probably should be rewritten to use debugfs? -- Ted]

I suppose that given that we're creating a spot in debugfs for the jbd2 debug code, yes, this also should be moved over.

But the jbd2 debug debugfs entries were kernel-wide whereas this is per-superblock. I think.

I take this comment as we still keep the stats info in proc fs...

Quoted 4 lines

That email from Alex contains pretty important information. I suggest that it be added to the changelog after accuracy-checking. Addition to Documentation/filesystems/ext4.txt would be good.

Done.

I hope Alex can help address the rest of comments.

Quoted 621 lines
Quoted 9 lines

--

Index: linux-2.6.22-rc4/include/linux/jbd2.h
===================================================================

include/linux/jbd2.h +0 -0
@@ -408,6 +408,16 @@
 };
 

include/linux/jbd2.h line 419

Quoted 10 lines
include/linux/jbd2.h +9 -0
 
+/*
+ * Some stats for checkpoint phase
+ */
+struct transaction_chp_stats_s {
+	unsigned long		cs_chp_time;
+	unsigned long		cs_forced_to_close;
+	unsigned long		cs_written;
+	unsigned long		cs_dropped;
+};

It would be nice to document what units all these fields are in. Jiffies, I assume.

Quoted 57 lines
include/linux/jbd2.h +43 -0
 /* The transaction_t type is the guts of the journaling mechanism.  It
  * tracks a compound transaction through its various states:
  *
@@ -543,6 +553,21 @@
 	spinlock_t		t_handle_lock;
 
 	/*
+	 * Longest time some handle had to wait for running transaction
+	 */
+	unsigned long		t_max_wait;
+
+	/*
+	 * When transaction started
+	 */
+	unsigned long		t_start;
+
+	/*
+	 * Checkpointing stats [j_checkpoint_sem]
+	 */
+	struct transaction_chp_stats_s t_chp_stats;
+
+	/*
 	 * Number of outstanding updates running on this transaction
 	 * [t_handle_lock]
 	 */
@@ -573,6 +598,57 @@
 
 };
 
+struct transaction_run_stats_s {
+	unsigned long		rs_wait;
+	unsigned long		rs_running;
+	unsigned long		rs_locked;
+	unsigned long		rs_flushing;
+	unsigned long		rs_logging;
+
+	unsigned long		rs_handle_count;
+	unsigned long		rs_blocks;
+	unsigned long		rs_blocks_logged;
+};
+
+struct transaction_stats_s
+{
+	int 			ts_type;
+	unsigned long		ts_tid;
+	union {
+		struct transaction_run_stats_s run;
+		struct transaction_chp_stats_s chp;
+	} u;
+};
+
+#define JBD2_STATS_RUN		1
+#define JBD2_STATS_CHECKPOINT	2
+
+#define ts_wait			u.run.rs_wait
+#define ts_running		u.run.rs_running
+#define ts_locked		u.run.rs_locked

include/linux/jbd2.h line 638

Quoted 10 lines
include/linux/jbd2.h +10 -0
+#define ts_flushing		u.run.rs_flushing
+#define ts_logging		u.run.rs_logging
+#define ts_handle_count		u.run.rs_handle_count
+#define ts_blocks		u.run.rs_blocks
+#define ts_blocks_logged	u.run.rs_blocks_logged
+
+#define ts_chp_time		u.chp.cs_chp_time
+#define ts_forced_to_close	u.chp.cs_forced_to_close
+#define ts_written		u.chp.cs_written
+#define ts_dropped		u.chp.cs_dropped

That's a bit sleazy. We can drop the "u" from 'struct transaction_stats_s' and make it an anonymous union, then open-code foo.run.rs_wait everywhere.

But that's a bit sleazy too, because the reader of the code would not know that a write to foo.run.rs_wait will stomp on the value of foo.chp.cs_chp_time.

So to minimize reader confusion it would be best I think to just open-code the full u.run.rs_wait at all code-sites.

The macros above are the worst possible choice: they hide information from the code-reader just to save the code-writer a bit of typing. But we very much want to optimise for code-readers, not for code-writers.

include/linux/jbd2.h line 640

Quoted 1 line
include/linux/jbd2.h +1 -0
+#define CURRENT_MSECS		(jiffies_to_msecs(jiffies))

hm, that isn't something which should be in an ext4 header file. And it shouldn't be in UPPER CASE and it shouldn't pretend to be a constant (or a global scalar).

IOW: yuk.

How's about raising a separate, standalone patch which creates a new kernel-wide coded-in-C function such as

unsigned long jiffies_in_msecs(void);

? (That's assuming we don't already have one. Most likely we have seven of them hiding in various dark corners).

include/linux/jbd2.h line 650

Quoted 9 lines
include/linux/jbd2.h +9 -0
+static inline unsigned int
+jbd2_time_diff(unsigned int start, unsigned int end)
+{
+	if (unlikely(start > end))
+		end = end + (~0UL - start);
+	else
+		end -= start;
+	return end;
+}

I don't know what this does and I am disinclined to reverse-engineer its uncommentedness. But it looks like something which should be in some kernel-wide header file?

Quoted 18 lines
include/linux/jbd2.h +6 -0
 /**
  * struct journal_s - The journal_s type is the concrete type associated with
  *     journal_t.
@@ -634,6 +710,12 @@
  * @j_wbufsize: maximum number of buffer_heads allowed in j_wbuf, the
  *	number that will fit in j_blocksize
  * @j_last_sync_writer: most recent pid which did a synchronous write
+ * @j_history: Buffer storing the transactions statistics history
+ * @j_history_max: Maximum number of transactions in the statistics history
+ * @j_history_cur: Current number of transactions in the statistics history
+ * @j_history_lock: Protect the transactions statistics history
+ * @j_proc_entry: procfs entry for the jbd statistics directory
+ * @j_stats: Overall statistics
  * @j_private: An opaque pointer to fs-private information.
  */
 
@@ -826,6 +908,16 @@
 	pid_t			j_last_sync_writer;

include/linux/jbd2.h line 918

Quoted 10 lines
include/linux/jbd2.h +8 -0
 
 	/*
+	 * Journal statistics
+	 */
+	struct transaction_stats_s *j_history;
+	int			j_history_max;
+	int			j_history_cur;
+	spinlock_t		j_history_lock;
+	struct proc_dir_entry	*j_proc_entry;
+	struct transaction_stats_s j_stats;

hm, we seem to be documenting the struct journal_s fields at two different places, possibly with duplication.

Documenting the fields at the top in kernedoc-style drives me wild, frankly. We really really do want to be able to see the description of each field (which is vital information for understanding the code!) right there at the definition site.

But at least it's documented somewhere.

Quoted 9 lines
include/linux/jbd2.h +1 -0
+	/*
 	 * An opaque pointer to fs-private information.  ext3 puts its
 	 * superblock pointer here
 	 */

Index: linux-2.6.22-rc4/fs/jbd2/transaction.c
===================================================================

fs/jbd2/transaction.c line 63

Quoted 5 lines
fs/jbd2/transaction.c +2 -0
@@ -59,6 +59,8 @@
 
 	J_ASSERT(journal->j_running_transaction == NULL);
 	journal->j_running_transaction = transaction;
+	transaction->t_max_wait = 0;
+	transaction->t_start = CURRENT_MSECS;

Ah, it's in milliseconds!

Good, albeit somewhat inefficient. Usually we'd accumulate jiffies and convert to/from milliseconds at the user interface boundary.

Quoted 29 lines
fs/jbd2/checkpoint.c +4 -1
@@ -232,7 +232,8 @@
  * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  */
 static int __process_buffer(journal_t *journal, struct journal_head *jh,
-			struct buffer_head **bhs, int *batch_count)
+			struct buffer_head **bhs, int *batch_count,
+			transaction_t *transaction)
 {
 	struct buffer_head *bh = jh2bh(jh);
 	int ret = 0;
@@ -250,6 +251,7 @@
 		transaction_t *t = jh->b_transaction;
 		tid_t tid = t->t_tid;
 
+		transaction->t_chp_stats.cs_forced_to_close++;
 		spin_unlock(&journal->j_list_lock);
 		jbd_unlock_bh_state(bh);
 		jbd2_log_start_commit(journal, tid);
@@ -279,6 +281,7 @@
 		bhs[*batch_count] = bh;
 		__buffer_relink_io(jh);
 		jbd_unlock_bh_state(bh);
+		transaction->t_chp_stats.cs_written++;
 		(*batch_count)++;
 		if (*batch_count == NR_BATCH) {
 			spin_unlock(&journal->j_list_lock);

fs/jbd2/checkpoint.c line 329

Quoted 5 lines
fs/jbd2/checkpoint.c +2 -0
@@ -322,6 +325,8 @@
 	if (!journal->j_checkpoint_transactions)
 		goto out;
 	transaction = journal->j_checkpoint_transactions;
+	if (transaction->t_chp_stats.cs_chp_time == 0)
+		transaction->t_chp_stats.cs_chp_time = CURRENT_MSECS;

Oh dear, I have no clue what cs_chp_time represents. Number of milliseconds for this transaction, I guess. In which case we must be either a) triggering stats collection via some user interface of b) averaging these on an ongoing basis or c) something else.

Let me read on...

Quoted 37 lines
fs/jbd2/checkpoint.c +15 -1
 	this_tid = transaction->t_tid;
 restart:
 	/*
@@ -346,7 +351,8 @@
 				retry = 1;
 				break;
 			}
-			retry = __process_buffer(journal, jh, bhs,&batch_count);
+			retry = __process_buffer(journal, jh, bhs, &batch_count,
+						 transaction);
 			if (!retry && lock_need_resched(&journal->j_list_lock)){
 				spin_unlock(&journal->j_list_lock);
 				retry = 1;
@@ -668,6 +674,8 @@
 
 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
 {
+	struct transaction_stats_s stats;
+
 	assert_spin_locked(&journal->j_list_lock);
 	if (transaction->t_cpnext) {
 		transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
@@ -693,5 +701,26 @@
 	J_ASSERT(journal->j_running_transaction != transaction);
 
 	jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
+
+	/*
+	 * File the transaction for history
+	 */
+	if (transaction->t_chp_stats.cs_written != 0 ||
+			transaction->t_chp_stats.cs_chp_time != 0) {
+		stats.ts_type = JBD2_STATS_CHECKPOINT;
+		stats.ts_tid = transaction->t_tid;
+		stats.u.chp = transaction->t_chp_stats;
+		if (stats.ts_chp_time)
+			stats.ts_chp_time =

fs/jbd2/checkpoint.c line 724

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+				jbd2_time_diff(stats.ts_chp_time,
+						CURRENT_MSECS);
+		spin_lock(&journal->j_history_lock);
+		memcpy(journal->j_history + journal->j_history_cur, &stats,
+				sizeof(stats));
+		if (++journal->j_history_cur == journal->j_history_max)
+			journal->j_history_cur = 0;
+		spin_unlock(&journal->j_history_lock);
+	}
+

Ah, it's a per-journal ringbuffer. Fair enough.

Quoted 97 lines
fs/jbd2/checkpoint.c +97 -0
+struct jbd2_stats_proc_session {
+	journal_t *journal;
+	struct transaction_stats_s *stats;
+	int start;
+	int max;
+};
+
+static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
+					struct transaction_stats_s *ts,
+					int first)
+{
+	if (ts == s->stats + s->max)
+		ts = s->stats;
+	if (!first && ts == s->stats + s->start)
+		return NULL;
+	while (ts->ts_type == 0) {
+		ts++;
+		if (ts == s->stats + s->max)
+			ts = s->stats;
+		if (ts == s->stats + s->start)
+			return NULL;
+	}
+	return ts;
+
+}
+
+static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
+{
+	struct jbd2_stats_proc_session *s = seq->private;
+	struct transaction_stats_s *ts;
+	int l = *pos;
+
+	if (l == 0)
+		return SEQ_START_TOKEN;
+	ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
+	if (!ts)
+		return NULL;
+	l--;
+	while (l) {
+		ts = jbd2_history_skip_empty(s, ++ts, 0);
+		if (!ts)
+			break;
+		l--;
+	}
+	return ts;
+}
+
+static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	struct jbd2_stats_proc_session *s = seq->private;
+	struct transaction_stats_s *ts = v;
+
+	++*pos;
+	if (v == SEQ_START_TOKEN)
+		return jbd2_history_skip_empty(s, s->stats + s->start, 1);
+	else
+		return jbd2_history_skip_empty(s, ++ts, 0);
+}
+
+static int jbd2_seq_history_show(struct seq_file *seq, void *v)
+{
+	struct transaction_stats_s *ts = v;
+	if (v == SEQ_START_TOKEN) {
+		seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
+				"%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
+				"wait", "run", "lock", "flush", "log", "hndls",
+				"block", "inlog", "ctime", "write", "drop",
+				"close");
+		return 0;
+	}
+	if (ts->ts_type == JBD2_STATS_RUN)
+		seq_printf(seq, "%-4s %-5lu %-5lu %-5lu %-5lu %-5lu %-5lu "
+				"%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
+				ts->ts_wait, ts->ts_running, ts->ts_locked,
+				ts->ts_flushing, ts->ts_logging,
+				ts->ts_handle_count, ts->ts_blocks,
+				ts->ts_blocks_logged);
+	else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
+		seq_printf(seq, "%-4s %-5lu %48s %-5lu %-5lu %-5lu %-5lu\n",
+				"C", ts->ts_tid, " ", ts->ts_chp_time,
+				ts->ts_written, ts->ts_dropped,
+				ts->ts_forced_to_close);
+	else
+		J_ASSERT(0);
+	return 0;
+}
+
+static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
+{
+}
+
+static struct seq_operations jbd2_seq_history_ops = {
+	.start  = jbd2_seq_history_start,
+	.next   = jbd2_seq_history_next,
+	.stop   = jbd2_seq_history_stop,
+	.show   = jbd2_seq_history_show,
+};

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+
+static int jbd2_seq_history_open(struct inode *inode, struct file *file)
+{
+	journal_t *journal = PDE(inode)->data;
+	struct jbd2_stats_proc_session *s;
+	int rc, size;
+
+	s = kmalloc(sizeof(*s), GFP_KERNEL);
+	if (s == NULL)
+		return -EIO;

ENOMEM.

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	size = sizeof(struct transaction_stats_s) * journal->j_history_max;
+	s->stats = kmalloc(size, GFP_KERNEL);
+	if (s == NULL) {
+		kfree(s);
+		return -EIO;

ENOMEM.

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+	}
+	spin_lock(&journal->j_history_lock);
+	memcpy(s->stats, journal->j_history, size);
+	s->max = journal->j_history_max;
+	s->start = journal->j_history_cur % s->max;
+	spin_unlock(&journal->j_history_lock);
+
+	rc = seq_open(file, &jbd2_seq_history_ops);
+	if (rc == 0) {
+		struct seq_file *m = (struct seq_file *)file->private_data;

unneeded cast of void*

fs/jbd2/checkpoint.c

Quoted 12 lines
fs/jbd2/checkpoint.c +12 -0
+		m->private = s;
+	} else {
+		kfree(s->stats);
+		kfree(s);
+	}
+	return rc;
+
+}
+
+static int jbd2_seq_history_release(struct inode *inode, struct file *file)
+{
+	struct seq_file *seq = (struct seq_file *)file->private_data;

unneeded cast of void*

Quoted 8 lines
fs/jbd2/checkpoint.c +6 -0
+	struct jbd2_stats_proc_session *s = seq->private;
+	kfree(s->stats);
+	kfree(s);
+	return seq_release(inode, file);
+}
+
 
...

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +9 -0
 
+static int jbd2_seq_info_open(struct inode *inode, struct file *file)
+{
+	journal_t *journal = PDE(inode)->data;
+	struct jbd2_stats_proc_session *s;
+	int rc, size;
+
+	s = kmalloc(sizeof(*s), GFP_KERNEL);
+	if (s == NULL)
+		return -EIO;

ENOMEM

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	size = sizeof(struct transaction_stats_s);
+	s->stats = kmalloc(size, GFP_KERNEL);
+	if (s == NULL) {
+		kfree(s);
+		return -EIO;

ENOMEM

Quoted 11 lines
fs/jbd2/checkpoint.c +11 -0
+	}
+	spin_lock(&journal->j_history_lock);
+	memcpy(s->stats, &journal->j_stats, size);
+	s->journal = journal;
+	spin_unlock(&journal->j_history_lock);
+
+	rc = seq_open(file, &jbd2_seq_info_ops);
+	if (rc == 0) {
+		struct seq_file *m = (struct seq_file *)file->private_data;
+		m->private = s;
+	} else {

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+		kfree(s->stats);
+		kfree(s);
+	}
+	return rc;
+
+}
+
+static int jbd2_seq_info_release(struct inode *inode, struct file *file)
+{
+	struct seq_file *seq = (struct seq_file *)file->private_data;

Unneeded cast

fs/jbd2/checkpoint.c

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+	struct jbd2_stats_proc_session *s = seq->private;
+	kfree(s->stats);
+	kfree(s);
+	return seq_release(inode, file);
+}

We would usually put a blanck line betwee end-of-locals and start-of-code.

fs/jbd2/checkpoint.c

Quoted 9 lines
fs/jbd2/checkpoint.c +9 -0
+static struct file_operations jbd2_seq_info_fops = {
+	.owner		= THIS_MODULE,
+	.open           = jbd2_seq_info_open,
+	.read           = seq_read,
+	.llseek         = seq_lseek,
+	.release        = jbd2_seq_info_release,
+};
+
+static struct proc_dir_entry *proc_jbd2_stats = NULL;

Unneeded (and wasteful) initialisation (checkpatch should catch this)

fs/jbd2/checkpoint.c

Quoted 3 lines
fs/jbd2/checkpoint.c +3 -0
+static void jbd2_stats_proc_init(journal_t *journal)
+{
+	char name[64];

This should have size BDEVNAME_SIZE.

Quoted 12 lines
fs/jbd2/checkpoint.c +12 -0
+	snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
+	journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
+	if (journal->j_proc_entry) {
+		struct proc_dir_entry *p;
+		p = create_proc_entry("history", S_IRUGO,
+				journal->j_proc_entry);
+		if (p) {
+			p->proc_fops = &jbd2_seq_history_fops;
+			p->data = journal;
+			p = create_proc_entry("info", S_IRUGO,
+						journal->j_proc_entry);
+			if (p) {

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+				p->proc_fops = &jbd2_seq_info_fops;
+				p->data = journal;
+			}
+		}
+	}
+}
+
+static void jbd2_stats_proc_exit(journal_t *journal)
+{
+	char name[64];

ditto

Quoted 13 lines
fs/jbd2/checkpoint.c +13 -0
+	snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
+	remove_proc_entry("info", journal->j_proc_entry);
+	remove_proc_entry("history", journal->j_proc_entry);
+	remove_proc_entry(name, proc_jbd2_stats);
+}
+
+static void journal_init_stats(journal_t *journal)
+{
+	int size;
+
+	if (proc_jbd2_stats == NULL)
+		return;
+

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+	journal->j_history_max = 100;
+	size = sizeof(struct transaction_stats_s) * journal->j_history_max;
+	journal->j_history = kmalloc(size, GFP_KERNEL);
+	if (journal->j_history == NULL) {
+		journal->j_history_max = 0;
+		return;
+	}
+	memset(journal->j_history, 0, size);
+	spin_lock_init(&journal->j_history_lock);
+}

So the "100" is not alterable at present although the code is pretty much set up to do this (code which alters the size of the history buffer will need to take j_history_lock).

fs/jbd2/checkpoint.c

Quoted 1 line
fs/jbd2/checkpoint.c +1 -0
+	journal_init_stats(journal);

This is all a heck of a lot of new code. Should we plan to make it Kconfigurable?

Quoted 1 line

+#if defined(CONFIG_PROC_FS)

#ifdef CONFIG_PROC_FS

would be more typical

Quoted 1 line

+#define JBD2_STATS_PROC_NAME "fs/jbd2"

hm, now what's going one here. Maybe I misiunderstood, and the stats are not per-superblock at all. That would be a shame.

Quoted 5 lines
fs/jbd2/checkpoint.c +5 -0
+static void __init jbd2_create_jbd_stats_proc_entry(void)
+{
+	proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
+}
+

fs/jbd2/checkpoint.c

Quoted 10 lines
fs/jbd2/checkpoint.c +10 -0
+static void __exit jbd2_remove_jbd_stats_proc_entry(void)
+{
+	if (proc_jbd2_stats)
+		remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
+}
+
+#else
+
+#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
+#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)

These could be coded in C. Remember: only use macros for things which _cannot_ be coded in C.

Quoted 20 lines
fs/jbd2/checkpoint.c +4 -0
+#endif
+
 struct kmem_cache *jbd2_handle_cache;
 
 static int __init journal_init_handle_cache(void)
@@ -2046,6 +2376,7 @@
 	if (ret != 0)
 		jbd2_journal_destroy_caches();
 	jbd2_create_debugfs_entry();
+	jbd2_create_jbd_stats_proc_entry();
 	return ret;
 }
 
@@ -2057,6 +2388,7 @@
 		printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
 #endif
 	jbd2_remove_debugfs_entry();
+	jbd2_remove_jbd_stats_proc_entry();
 	jbd2_journal_destroy_caches();
 }
#6 lore raw
In reply to Andrew Morton
Quoted 20 lines · Andrew Morton
Quoted 10 lines · On Tue, 10 Jul 2007 23:21:49 -0400 "Cédric Augonnet"…

2007/7/10, Andrew Morton <[email protected]>:

Hi all,

Quoted 6 lines
Quoted 5 lines
(quoted diff) +5 -0
+     size = sizeof(struct transaction_stats_s);
+     s->stats = kmalloc(size, GFP_KERNEL);
+     if (s == NULL) {
+             kfree(s);
+             return -EIO;
Quoted 7 lines
Quoted 1 line

ENOMEM

I'm sorry if i missed some point, but i just don't see the use of such a kfree here, not sure Andrew meant you should only return ENOMEM instead, but why issuing those kfree(NULL), instead of just a if (!s) return ENOMEM ?

Quoted 5 lines

You found a bug. It was meant to be

	if (s->stats == NULL)

The incremental fix patch is attached just FYI. It will be folded to the parent patch once the parent patch is being updated.

---
 fs/jbd2/journal.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jbd2/journal.c +1 -1
Index: linux-2.6.22/fs/jbd2/journal.c
===================================================================
@@ -751,7 +751,7 @@ static int jbd2_seq_history_open(struct 
 		return -EIO;
 	size = sizeof(struct transaction_stats_s) * journal->j_history_max;
 	s->stats = kmalloc(size, GFP_KERNEL);
-	if (s == NULL) {
+	if (s->stats == NULL) {
 		kfree(s);
 		return -EIO;
 	}

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to Mingming Cao
Quoted 10 lines · Mingming Cao

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

[ This probably should be rewritten to use debugfs? -- Ted]

I've started going through this one to clean it up to the point where it can go forward. It's been sitting at the top of the unstable portion of the patch queue for long enough, I think :)

I've converted the msecs to jiffies until the user boundary, changed the union #defines as suggested by Andrew, and various other little issues etc.

Remaining to do is a generic time-difference calculator (instead of jbd2_time_diff), and looking into whether it should be made a config option; I tend to think it should, but it's fairly well sprinkled through the code, so I'll see how well that works.

Also did we ever decided if this should go to debugfs?

Thanks,

-Eric

#10 lore raw
Quoted 8 lines · Eric Sandeen
Quoted 7 lines · Mingming Cao

[PATCH] jbd2 stats through procfs

The patch below updates the jbd stats patch to 2.6.20/jbd2. The initial patch was posted by Alex Tomas in December 2005
(http://marc.info/?l=linux-ext4&m=113538565128617&w=2).
It provides statistics via procfs such as transaction lifetime and size.

Quoted 8 lines
Quoted 3 lines

[ This probably should be rewritten to use debugfs? -- Ted]

I've started going through this one to clean it up to the point where it can go forward. It's been sitting at the top of the unstable portion of the patch queue for long enough, I think :)

Thanks!

Quoted 10 lines

I've converted the msecs to jiffies until the user boundary, changed the union #defines as suggested by Andrew, and various other little issues etc.

Remaining to do is a generic time-difference calculator (instead of jbd2_time_diff), and looking into whether it should be made a config option; I tend to think it should, but it's fairly well sprinkled through the code, so I'll see how well that works.

Also did we ever decided if this should go to debugfs?

I thought it was decided to keep it on procfs as debugfs is not always on...

Quoted 7 lines

Thanks,

(quoted diff) +0 -2
-Eric
-

To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html