]> granicus.if.org Git - sysstat/blob - tapestat.h
tapestat: Check fscanf() return value
[sysstat] / tapestat.h
1 /*
2  * tapestat: report tape statistics
3  * (C) 2015 Hewlett-Packard Development Company, L.P.
4  * 
5  * Initial revision by Shane M. SEYMOUR (shane.seymour <at> hp.com)
6  * Modified for sysstat by Sebastien GODARD (sysstat <at> orange.fr)
7  */
8
9 #ifndef _TAPESTAT_H
10 #define _TAPESTAT_H
11
12 /* T_: tapestat - D_: Display - F_: Flag */
13 #define T_D_TIMESTAMP           0x00001
14 #define T_D_KILOBYTES           0x00002
15 #define T_D_MEGABYTES           0x00004
16 #define T_D_OMIT_SINCE_BOOT     0x00008
17 #define T_D_ISO                 0x00010
18 #define T_D_ZERO_OMIT           0x00020
19
20 #define DISPLAY_TIMESTAMP(m)            (((m) & T_D_TIMESTAMP)       == T_D_TIMESTAMP)
21 #define DISPLAY_KILOBYTES(m)            (((m) & T_D_KILOBYTES)       == T_D_KILOBYTES)
22 #define DISPLAY_MEGABYTES(m)            (((m) & T_D_MEGABYTES)       == T_D_MEGABYTES)
23 #define DISPLAY_OMIT_SINCE_BOOT(m)      (((m) & T_D_OMIT_SINCE_BOOT) == T_D_OMIT_SINCE_BOOT)
24 #define DISPLAY_ISO(m)                  (((m) & T_D_ISO)             == T_D_ISO)
25 #define DISPLAY_ZERO_OMIT(m)            (((m) & T_D_ZERO_OMIT)       == T_D_ZERO_OMIT)
26
27
28 #define TAPE_STATS_VALID 1
29 #define TAPE_STATS_INVALID 0
30
31 #define SYSFS_CLASS_TAPE_DIR "/sys/class/scsi_tape"
32 #define TAPE_STAT_PATH "/sys/class/scsi_tape/st%i/stats/"
33
34 #define TAPE_STAT_FILE_VAL(A, B)                                        \
35         snprintf(filename, MAXPATHLEN, A, i);                           \
36         if ((fp = fopen(filename, "r")) != NULL) {                      \
37                 if (fscanf(fp, "%"PRId64, &tape_new_stats[i].B) != 1) { \
38                         tape_new_stats[i].valid = TAPE_STATS_INVALID;   \
39                 }                                                       \
40                 fclose(fp);                                             \
41         } else {                                                        \
42                 tape_new_stats[i].valid = TAPE_STATS_INVALID;           \
43                 continue;                                               \
44         }
45
46
47 /*
48  * A - tape_stats structure member name, e.g. read_count
49  * B - calc_stats structure member name, e.g. reads_per_second
50  *
51  * These macros are not selfcontained they depend on some other
52  * variables defined either as global or local to the function.
53  */
54
55 #define CALC_STAT_CNT(A, B)                                     \
56         if ((tape_new_stats[i].A == tape_old_stats[i].A) ||     \
57                 (duration <= 0)) {                              \
58                 stats->B = 0;                                   \
59         } else {                                                \
60                 temp = (double) (tape_new_stats[i].A -          \
61                         tape_old_stats[i].A)                    \
62                         / (((double) duration) / 1000);         \
63                 stats->B = (uint64_t) temp;                     \
64         }
65 #define CALC_STAT_KB(A, B)                                      \
66         if ((tape_new_stats[i].A == tape_old_stats[i].A) ||     \
67                 (duration <= 0)) {                              \
68                 stats->B = 0;                                   \
69         } else {                                                \
70                 temp = (double) (tape_new_stats[i].A -          \
71                         tape_old_stats[i].A)                    \
72                         / (((double) duration) / 1000.0);       \
73                 stats->B = (uint64_t) (temp / 1024.0);          \
74         }
75
76 #define TAPE_MAX_PCT 999
77
78 #define CALC_STAT_PCT(A, B)                                             \
79         if ((tape_new_stats[i].A == tape_old_stats[i].A) ||             \
80                 (duration <= 0)) {                                      \
81                 stats->B = 0;                                           \
82         } else {                                                        \
83                 temp = (double) (tape_new_stats[i].A -                  \
84                         tape_old_stats[i].A)                            \
85                         / (((double) duration));                        \
86                 stats->B = (uint64_t) (100.0 * temp / 1000000.0);       \
87                 if (stats->B > TAPE_MAX_PCT)                            \
88                         stats->B = TAPE_MAX_PCT;                                \
89         }
90
91 struct tape_stats {
92         uint64_t read_time;
93         uint64_t write_time;
94         uint64_t other_time;
95         uint64_t read_bytes;
96         uint64_t write_bytes;
97         uint64_t read_count;
98         uint64_t write_count;
99         uint64_t other_count;
100         uint64_t resid_count;
101         char valid;
102         struct timeval tv;
103 };
104 struct calc_stats {
105         uint64_t reads_per_second;
106         uint64_t writes_per_second;
107         uint64_t other_per_second;
108         uint64_t kbytes_read_per_second;
109         uint64_t kbytes_written_per_second;
110         uint64_t read_pct_wait;
111         uint64_t write_pct_wait;
112         uint64_t all_pct_wait;
113         uint64_t resids_per_second;
114 };
115
116 void tape_get_updated_stats(void);
117 void tape_gather_initial_stats(void);
118 void tape_check_tapes_and_realloc(void);
119 int get_max_tape_drives(void);
120 void tape_uninitialise(void);
121 void tape_initialise(void);
122 void tape_calc_one_stats(struct calc_stats *, int);
123 void tape_write_headings(void);
124 void tape_write_stats(struct calc_stats *, int);
125
126 #endif  /* _TAPESTAT_H */