1 Index: linux-2.6.9/drivers/scsi/Kconfig
2 ===================================================================
3 --- linux-2.6.9.orig/drivers/scsi/Kconfig 2007-08-02 09:26:17.000000000 +0200
4 +++ linux-2.6.9/drivers/scsi/Kconfig 2007-08-02 09:26:21.000000000 +0200
5 @@ -61,6 +61,14 @@ config SCSI_DUMP
10 + bool "Enable SCSI disk I/O stats"
11 + depends on BLK_DEV_SD
14 + This enables SCSI disk I/O stats collection. You must also enable
15 + /proc file system support if you want this feature.
18 tristate "SCSI tape support"
20 Index: linux-2.6.9/drivers/scsi/sd.c
21 ===================================================================
22 --- linux-2.6.9.orig/drivers/scsi/sd.c 2007-08-02 09:26:17.000000000 +0200
23 +++ linux-2.6.9/drivers/scsi/sd.c 2007-08-02 09:29:31.000000000 +0200
26 #include "scsi_logging.h"
28 +#if (defined(CONFIG_SD_IOSTATS) && defined(CONFIG_PROC_FS))
29 +# include <linux/proc_fs.h>
30 +# include <linux/seq_file.h>
33 + unsigned long long iostat_size;
34 + unsigned long long iostat_count;
37 +#define IOSTAT_NCOUNTERS 16
39 + iostat_counter_t iostat_read_histogram[IOSTAT_NCOUNTERS];
40 + iostat_counter_t iostat_write_histogram[IOSTAT_NCOUNTERS];
41 + struct timeval iostat_timeval;
44 +iostat_stats_t **sd_iostats;
45 +spinlock_t sd_iostats_lock;
46 +struct proc_dir_entry *sd_iostats_procdir;
47 +char sd_iostats_procdir_name[] = "sd_iostats";
49 +extern void sd_iostats_init(void);
50 +extern void sd_iostats_init_disk(struct gendisk *);
51 +extern void sd_iostats_fini(void);
52 +extern void sd_iostats_bump(int disk, unsigned int nsect, int iswrite);
54 +static inline void sd_iostats_init(void) {}
55 +static inline void sd_iostats_init_disk(struct gendisk *disk) {}
56 +static inline void sd_iostats_fini(void) {}
57 +static inline void sd_iostats_bump(int disk, unsigned int nsect, int iswrite) {}
61 * More than enough for everybody ;) The huge number of majors
62 * is a leftover from 16bit dev_t days, we don't really need that
67 - * This is limited by the naming scheme enforced in sd_probe,
68 + * SD_MAX_DISKS is limited by the naming scheme enforced in sd_probe,
69 * add another character to it if you really need more disks.
71 + * SD_STATS must not be less than SD_MAX_DISKS and, is limited by 131072/8
72 + * for sd_iostats kmalloc (see kmalloc_sizes.h)
74 +#if (defined(CONFIG_SD_IOSTATS) && defined(CONFIG_PROC_FS))
75 +#define SD_STATS 16384
76 +#define SD_MAX_DISKS SD_STATS
78 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
82 * Time out in seconds for disks and Magneto-opticals (which are slower).
83 @@ -278,6 +318,9 @@ static int sd_init_command(struct scsi_c
84 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
85 disk->disk_name, (unsigned long long)block));
87 + sd_iostats_bump(scsi_disk(disk)->index, this_count,
88 + rq_data_dir(SCpnt->request) == WRITE);
91 * If we have a 1K hardware sectorsize, prevent access to single
92 * 512 byte sectors. In theory we could handle this - in fact
93 @@ -474,6 +517,7 @@ static int sd_open(struct inode *inode,
94 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
97 + sd_iostats_init_disk(disk);
101 @@ -500,8 +544,20 @@ static int sd_release(struct inode *inod
103 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
105 - if (!--sdkp->openers && sdev->removable) {
106 - if (scsi_block_when_processing_errors(sdev))
107 + if (!--sdkp->openers) {
109 + * Remove sd_iostats information about this disk
111 + if (sd_iostats_procdir != NULL) {
112 + remove_proc_entry(disk->disk_name, sd_iostats_procdir);
114 + if (sd_iostats != NULL) {
115 + if (sd_iostats[sdkp->index] != NULL) {
116 + kfree (sd_iostats[sdkp->index]);
117 + sd_iostats[sdkp->index] = NULL;
120 + if (sdev->removable && scsi_block_when_processing_errors(sdev))
121 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
124 @@ -1575,6 +1631,342 @@ static void sd_shutdown(struct device *d
128 +#if (defined(CONFIG_SD_IOSTATS) && defined(CONFIG_PROC_FS))
130 +sd_iostats_seq_show(struct seq_file *seq, void *v)
132 + struct timeval now;
133 + struct gendisk *disk;
134 + iostat_stats_t *stats;
135 + unsigned long long read_len;
136 + unsigned long long read_len_tot;
137 + unsigned long read_num;
138 + unsigned long read_num_tot;
139 + unsigned long long write_len;
140 + unsigned long long write_len_tot;
141 + unsigned long write_num;
142 + unsigned long write_num_tot;
146 + if (seq == NULL || seq->private == NULL) {
147 + printk(KERN_ERR "sd_iostats_seq_show: NULL disk\n");
151 + disk = seq->private;
153 + if (scsi_disk(disk) == NULL || (disk->flags & GENHD_FL_UP) == 0) {
154 + seq_printf(seq, "sd_iostats_seq_show: Device %s "
155 + "does not exist\n", disk->disk_name);
159 + if (sd_iostats == NULL) {
160 + printk(KERN_ERR "sd_iostats_seq_show: NULL stats array\n");
164 + stats = sd_iostats[scsi_disk(disk)->index];
165 + if (stats == NULL) {
166 + seq_printf(seq, "sd_iostats_seq_show: sd_iostats "
167 + "entry %d does not exist\n",
168 + scsi_disk(disk)->index);
172 + do_gettimeofday(&now);
173 + now.tv_sec -= stats->iostat_timeval.tv_sec;
174 + now.tv_usec -= stats->iostat_timeval.tv_usec;
175 + if (now.tv_usec < 0) {
176 + now.tv_usec += 1000000;
180 + /* this sampling races with updates */
181 + seq_printf(seq, "index: %lu snapshot_time: %lu.%06lu\n",
182 + scsi_disk(disk)->index, now.tv_sec, now.tv_usec);
184 + for (i = IOSTAT_NCOUNTERS - 1; i > 0; i--)
185 + if (stats->iostat_read_histogram[i].iostat_count != 0 ||
186 + stats->iostat_write_histogram[i].iostat_count != 0)
190 + seq_printf(seq, "%8s %8s %12s %8s %12s\n", "size",
191 + "reads", "total", "writes", "total");
193 + read_len_tot = write_len_tot = 0;
194 + read_num_tot = write_num_tot = 0;
195 + for (i = 0; i <= maxi; i++) {
196 + read_len = stats->iostat_read_histogram[i].iostat_size;
197 + read_len_tot += read_len;
198 + read_num = stats->iostat_read_histogram[i].iostat_count;
199 + read_num_tot += read_num;
201 + write_len = stats->iostat_write_histogram[i].iostat_size;
202 + write_len_tot += write_len;
203 + write_num = stats->iostat_write_histogram[i].iostat_count;
204 + write_num_tot += write_num;
206 + seq_printf (seq, "%8d %8lu %12llu %8lu %12llu\n",
207 + 512<<i, read_num, read_len, write_num, write_len);
210 + seq_printf(seq, "%8s %8lu %12llu %8lu %12llu\n", "total",
211 + read_num_tot, read_len_tot,
212 + write_num_tot, write_len_tot);
217 +sd_iostats_seq_start(struct seq_file *p, loff_t *pos)
219 + return (*pos == 0) ? (void *)1 : NULL;
223 +sd_iostats_seq_next(struct seq_file *p, void *v, loff_t *pos)
230 +sd_iostats_seq_stop(struct seq_file *p, void *v)
234 +static struct seq_operations sd_iostats_seqops = {
235 + .start = sd_iostats_seq_start,
236 + .stop = sd_iostats_seq_stop,
237 + .next = sd_iostats_seq_next,
238 + .show = sd_iostats_seq_show,
242 +sd_iostats_seq_open (struct inode *inode, struct file *file)
246 + rc = seq_open(file, &sd_iostats_seqops);
250 + ((struct seq_file *)file->private_data)->private = PDE(inode)->data;
255 +sd_iostats_seq_write(struct file *file, const char *buffer,
256 + size_t len, loff_t *off)
258 + struct seq_file *seq = file->private_data;
259 + struct gendisk *disk = seq->private;
260 + iostat_stats_t *stats = sd_iostats[scsi_disk(disk)->index];
261 + unsigned long flags;
264 + spin_lock_irqsave (&sd_iostats_lock, flags);
265 + memset (stats, 0, sizeof(*stats));
266 + do_gettimeofday(&stats->iostat_timeval);
267 + spin_unlock_irqrestore (&sd_iostats_lock, flags);
272 +static struct file_operations sd_iostats_proc_fops = {
273 + .owner = THIS_MODULE,
274 + .open = sd_iostats_seq_open,
276 + .write = sd_iostats_seq_write,
277 + .llseek = seq_lseek,
278 + .release = seq_release,
281 +extern struct proc_dir_entry *proc_scsi;
284 +sd_iostats_init(void)
288 + spin_lock_init(&sd_iostats_lock);
290 + sd_iostats = kmalloc(SD_STATS * sizeof(iostat_stats_t *), GFP_KERNEL);
291 + if (sd_iostats == NULL) {
292 + printk(KERN_WARNING "Can't keep sd iostats: "
293 + "ENOMEM allocating stats array size %ld\n",
294 + SD_STATS * sizeof(iostat_stats_t *));
298 + for (i = 0; i < SD_STATS; i++)
299 + sd_iostats[i] = NULL;
301 + if (proc_scsi == NULL) {
302 + printk(KERN_WARNING "No access to sd iostats: "
303 + "proc_scsi is NULL\n");
307 + sd_iostats_procdir = create_proc_entry(sd_iostats_procdir_name,
308 + S_IFDIR | S_IRUGO | S_IXUGO,
310 + if (sd_iostats_procdir == NULL) {
311 + printk(KERN_WARNING "No access to sd iostats: "
312 + "can't create /proc/scsi/%s\n", sd_iostats_procdir_name);
318 +sd_iostats_init_disk(struct gendisk *disk)
320 + struct proc_dir_entry *pde;
321 + unsigned long flags;
322 + iostat_stats_t *stats;
324 + if (sd_iostats == NULL ||
325 + sd_iostats_procdir == NULL)
328 + if (scsi_disk(disk)->index > SD_STATS) {
329 + printk(KERN_ERR "sd_iostats_init_disk: "
330 + "unexpected disk index %d(%d)\n",
331 + scsi_disk(disk)->index, SD_STATS);
335 + if (sd_iostats[scsi_disk(disk)->index] != NULL)
338 + stats = kmalloc(sizeof(*stats), GFP_KERNEL);
339 + if (stats == NULL) {
340 + printk(KERN_WARNING "Can't keep %s iostats: "
341 + "ENOMEM allocating stats size %ld\n",
342 + disk->disk_name, sizeof(*stats));
346 + memset (stats, 0, sizeof(*stats));
347 + do_gettimeofday(&stats->iostat_timeval);
349 + spin_lock_irqsave(&sd_iostats_lock, flags);
351 + if (sd_iostats[scsi_disk(disk)->index] != NULL) {
352 + spin_unlock_irqrestore(&sd_iostats_lock, flags);
357 + sd_iostats[scsi_disk(disk)->index] = stats;
359 + spin_unlock_irqrestore(&sd_iostats_lock, flags);
361 + pde = create_proc_entry(disk->disk_name, S_IRUGO | S_IWUSR,
362 + sd_iostats_procdir);
364 + printk(KERN_WARNING "Can't create /proc/scsi/%s/%s\n",
365 + sd_iostats_procdir_name, disk->disk_name);
367 + pde->proc_fops = &sd_iostats_proc_fops;
372 +static void sd_devname(unsigned int disknum, char *buffer)
375 + sprintf(buffer, "sd%c", 'a' + disknum);
380 + * For larger numbers of disks, we need to go to a new
383 + min1 = disknum / 26;
384 + min2 = disknum % 26;
385 + sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
390 +sd_iostats_fini(void)
395 + if (sd_iostats_procdir != NULL) {
396 + for (i = 0; i < SD_STATS; i++) {
397 + sd_devname(i, name);
398 + remove_proc_entry(name, sd_iostats_procdir);
401 + if (proc_scsi == NULL) {
402 + printk(KERN_ERR "sd_iostats_fini: proc_scsi NULL\n");
405 + remove_proc_entry(sd_iostats_procdir_name,
408 + sd_iostats_procdir = NULL;
411 + if (sd_iostats != NULL) {
412 + for (i = 0; i < SD_STATS; i++) {
413 + if (sd_iostats[i] != NULL)
414 + kfree (sd_iostats[i]);
423 +sd_iostats_bump(int disk, unsigned int nsect, int iswrite)
425 + iostat_stats_t *stats;
426 + iostat_counter_t *counter;
429 + unsigned long irqflags;
431 + if (sd_iostats == NULL)
434 + if (disk < 0 || disk >= SD_STATS) {
435 + printk(KERN_ERR "sd_iostats_bump: unexpected disk index %d([0-%d])\n",
440 + for (bucket = 0, tmp = nsect; tmp > 1; bucket++)
443 + if (bucket >= IOSTAT_NCOUNTERS) {
444 + printk (KERN_ERR "sd_iostats_bump: nsect %d too big\n", nsect);
448 + spin_lock_irqsave(&sd_iostats_lock, irqflags);
450 + stats = sd_iostats[disk];
451 + if (stats != NULL) {
452 + counter = iswrite ?
453 + &stats->iostat_write_histogram[bucket] :
454 + &stats->iostat_read_histogram[bucket];
456 + counter->iostat_size += nsect;
457 + counter->iostat_count++;
460 + spin_unlock_irqrestore(&sd_iostats_lock, irqflags);
465 * init_sd - entry point for this driver (both when built in or when
467 @@ -1584,6 +1976,7 @@ static void sd_shutdown(struct device *d
468 static int __init init_sd(void)
473 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
475 @@ -1594,7 +1987,10 @@ static int __init init_sd(void)
479 - return scsi_register_driver(&sd_template.gendrv);
480 + rc = scsi_register_driver(&sd_template.gendrv);
487 @@ -1608,6 +2004,7 @@ static void __exit exit_sd(void)
489 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
492 scsi_unregister_driver(&sd_template.gendrv);
493 for (i = 0; i < SD_MAJORS; i++)
494 unregister_blkdev(sd_major(i), "sd");
495 Index: linux-2.6.9/drivers/scsi/scsi_proc.c
496 ===================================================================
497 --- linux-2.6.9.orig/drivers/scsi/scsi_proc.c 2007-08-02 09:26:17.000000000 +0200
498 +++ linux-2.6.9/drivers/scsi/scsi_proc.c 2007-08-02 09:26:21.000000000 +0200
500 /* 4K page size, but our output routines, use some slack for overruns */
501 #define PROC_BLOCK_SIZE (3*1024)
503 -static struct proc_dir_entry *proc_scsi;
504 +struct proc_dir_entry *proc_scsi;
505 +EXPORT_SYMBOL(proc_scsi);
507 /* Protect sht->present and sht->proc_dir */
508 static DECLARE_MUTEX(global_host_template_sem);