@@ -276,56 +276,39 @@ static ssize_t target_stat_lu_state_bit_show(struct config_item *item,
276276 return snprintf (page , PAGE_SIZE , "exposed\n" );
277277}
278278
279- static ssize_t target_stat_lu_num_cmds_show (struct config_item * item ,
280- char * page )
281- {
282- struct se_device * dev = to_stat_lu_dev (item );
283- struct se_dev_io_stats * stats ;
284- unsigned int cpu ;
285- u32 cmds = 0 ;
286-
287- for_each_possible_cpu (cpu ) {
288- stats = per_cpu_ptr (dev -> stats , cpu );
289- cmds += stats -> total_cmds ;
290- }
291-
292- /* scsiLuNumCommands */
293- return snprintf (page , PAGE_SIZE , "%u\n" , cmds );
294- }
295-
296- static ssize_t target_stat_lu_read_mbytes_show (struct config_item * item ,
297- char * page )
298- {
299- struct se_device * dev = to_stat_lu_dev (item );
300- struct se_dev_io_stats * stats ;
301- unsigned int cpu ;
302- u32 bytes = 0 ;
303-
304- for_each_possible_cpu (cpu ) {
305- stats = per_cpu_ptr (dev -> stats , cpu );
306- bytes += stats -> read_bytes ;
307- }
308-
309- /* scsiLuReadMegaBytes */
310- return snprintf (page , PAGE_SIZE , "%u\n" , bytes >> 20 );
311- }
312-
313- static ssize_t target_stat_lu_write_mbytes_show (struct config_item * item ,
314- char * page )
315- {
316- struct se_device * dev = to_stat_lu_dev (item );
317- struct se_dev_io_stats * stats ;
318- unsigned int cpu ;
319- u32 bytes = 0 ;
320-
321- for_each_possible_cpu (cpu ) {
322- stats = per_cpu_ptr (dev -> stats , cpu );
323- bytes += stats -> write_bytes ;
324- }
325-
326- /* scsiLuWrittenMegaBytes */
327- return snprintf (page , PAGE_SIZE , "%u\n" , bytes >> 20 );
328- }
279+ #define per_cpu_stat_snprintf (stats_struct , prefix , field , shift ) \
280+ static ssize_t \
281+ per_cpu_stat_##prefix##_snprintf(struct stats_struct __percpu *per_cpu_stats, \
282+ char *page) \
283+ { \
284+ struct stats_struct *stats; \
285+ unsigned int cpu; \
286+ u64 sum = 0; \
287+ \
288+ for_each_possible_cpu(cpu) { \
289+ stats = per_cpu_ptr(per_cpu_stats, cpu); \
290+ sum += stats->field; \
291+ } \
292+ \
293+ return snprintf(page, PAGE_SIZE, "%llu\n", sum >> shift); \
294+ }
295+
296+ #define lu_show_per_cpu_stat (prefix , field , shift ) \
297+ per_cpu_stat_snprintf(se_dev_io_stats, prefix, field, shift); \
298+ static ssize_t \
299+ target_stat_##prefix##_show(struct config_item *item, char *page) \
300+ { \
301+ struct se_device *dev = to_stat_lu_dev(item); \
302+ \
303+ return per_cpu_stat_##prefix##_snprintf(dev->stats, page); \
304+ } \
305+
306+ /* scsiLuNumCommands */
307+ lu_show_per_cpu_stat (lu_num_cmds , total_cmds , 0 );
308+ /* scsiLuReadMegaBytes */
309+ lu_show_per_cpu_stat (lu_read_mbytes , read_bytes , 20 );
310+ /* scsiLuWrittenMegaBytes */
311+ lu_show_per_cpu_stat (lu_write_mbytes , write_bytes , 20 );
329312
330313static ssize_t target_stat_lu_resets_show (struct config_item * item , char * page )
331314{
@@ -623,53 +606,30 @@ static ssize_t target_stat_tgt_port_port_index_show(struct config_item *item,
623606 return ret ;
624607}
625608
626- static ssize_t target_stat_tgt_port_in_cmds_show (struct config_item * item ,
627- char * page )
628- {
629- struct se_lun * lun = to_stat_tgt_port (item );
630- struct se_device * dev ;
631- ssize_t ret = - ENODEV ;
632-
633- rcu_read_lock ();
634- dev = rcu_dereference (lun -> lun_se_dev );
635- if (dev )
636- ret = snprintf (page , PAGE_SIZE , "%lu\n" ,
637- atomic_long_read (& lun -> lun_stats .cmd_pdus ));
638- rcu_read_unlock ();
639- return ret ;
640- }
641-
642- static ssize_t target_stat_tgt_port_write_mbytes_show (struct config_item * item ,
643- char * page )
644- {
645- struct se_lun * lun = to_stat_tgt_port (item );
646- struct se_device * dev ;
647- ssize_t ret = - ENODEV ;
648-
649- rcu_read_lock ();
650- dev = rcu_dereference (lun -> lun_se_dev );
651- if (dev )
652- ret = snprintf (page , PAGE_SIZE , "%u\n" ,
653- (u32 )(atomic_long_read (& lun -> lun_stats .rx_data_octets ) >> 20 ));
654- rcu_read_unlock ();
655- return ret ;
656- }
657-
658- static ssize_t target_stat_tgt_port_read_mbytes_show (struct config_item * item ,
659- char * page )
660- {
661- struct se_lun * lun = to_stat_tgt_port (item );
662- struct se_device * dev ;
663- ssize_t ret = - ENODEV ;
664-
665- rcu_read_lock ();
666- dev = rcu_dereference (lun -> lun_se_dev );
667- if (dev )
668- ret = snprintf (page , PAGE_SIZE , "%u\n" ,
669- (u32 )(atomic_long_read (& lun -> lun_stats .tx_data_octets ) >> 20 ));
670- rcu_read_unlock ();
671- return ret ;
672- }
609+ #define tgt_port_show_per_cpu_stat (prefix , field , shift ) \
610+ per_cpu_stat_snprintf(scsi_port_stats, prefix, field, shift); \
611+ static ssize_t \
612+ target_stat_##prefix##_show(struct config_item *item, char *page) \
613+ { \
614+ struct se_lun *lun = to_stat_tgt_port(item); \
615+ struct se_device *dev; \
616+ int ret; \
617+ \
618+ rcu_read_lock(); \
619+ dev = rcu_dereference(lun->lun_se_dev); \
620+ if (!dev) { \
621+ rcu_read_unlock(); \
622+ return -ENODEV; \
623+ } \
624+ \
625+ ret = per_cpu_stat_##prefix##_snprintf(lun->lun_stats, page); \
626+ rcu_read_unlock(); \
627+ return ret; \
628+ }
629+
630+ tgt_port_show_per_cpu_stat (tgt_port_in_cmds , cmd_pdus , 0 );
631+ tgt_port_show_per_cpu_stat (tgt_port_write_mbytes , rx_data_octets , 20 );
632+ tgt_port_show_per_cpu_stat (tgt_port_read_mbytes , tx_data_octets , 20 );
673633
674634static ssize_t target_stat_tgt_port_hs_in_cmds_show (struct config_item * item ,
675635 char * page )
@@ -1035,92 +995,34 @@ static ssize_t target_stat_auth_att_count_show(struct config_item *item,
1035995 return ret ;
1036996}
1037997
1038- static ssize_t target_stat_auth_num_cmds_show (struct config_item * item ,
1039- char * page )
1040- {
1041- struct se_lun_acl * lacl = auth_to_lacl (item );
1042- struct se_node_acl * nacl = lacl -> se_lun_nacl ;
1043- struct se_dev_entry_io_stats * stats ;
1044- struct se_dev_entry * deve ;
1045- unsigned int cpu ;
1046- ssize_t ret ;
1047- u32 cmds = 0 ;
1048-
1049- rcu_read_lock ();
1050- deve = target_nacl_find_deve (nacl , lacl -> mapped_lun );
1051- if (!deve ) {
1052- rcu_read_unlock ();
1053- return - ENODEV ;
1054- }
1055-
1056- for_each_possible_cpu (cpu ) {
1057- stats = per_cpu_ptr (deve -> stats , cpu );
1058- cmds += stats -> total_cmds ;
1059- }
1060-
1061- /* scsiAuthIntrOutCommands */
1062- ret = snprintf (page , PAGE_SIZE , "%u\n" , cmds );
1063- rcu_read_unlock ();
1064- return ret ;
1065- }
1066-
1067- static ssize_t target_stat_auth_read_mbytes_show (struct config_item * item ,
1068- char * page )
1069- {
1070- struct se_lun_acl * lacl = auth_to_lacl (item );
1071- struct se_node_acl * nacl = lacl -> se_lun_nacl ;
1072- struct se_dev_entry_io_stats * stats ;
1073- struct se_dev_entry * deve ;
1074- unsigned int cpu ;
1075- ssize_t ret ;
1076- u32 bytes = 0 ;
1077-
1078- rcu_read_lock ();
1079- deve = target_nacl_find_deve (nacl , lacl -> mapped_lun );
1080- if (!deve ) {
1081- rcu_read_unlock ();
1082- return - ENODEV ;
1083- }
1084-
1085- for_each_possible_cpu (cpu ) {
1086- stats = per_cpu_ptr (deve -> stats , cpu );
1087- bytes += stats -> read_bytes ;
1088- }
1089-
1090- /* scsiAuthIntrReadMegaBytes */
1091- ret = snprintf (page , PAGE_SIZE , "%u\n" , bytes >> 20 );
1092- rcu_read_unlock ();
1093- return ret ;
1094- }
1095-
1096- static ssize_t target_stat_auth_write_mbytes_show (struct config_item * item ,
1097- char * page )
1098- {
1099- struct se_lun_acl * lacl = auth_to_lacl (item );
1100- struct se_node_acl * nacl = lacl -> se_lun_nacl ;
1101- struct se_dev_entry_io_stats * stats ;
1102- struct se_dev_entry * deve ;
1103- unsigned int cpu ;
1104- ssize_t ret ;
1105- u32 bytes = 0 ;
1106-
1107- rcu_read_lock ();
1108- deve = target_nacl_find_deve (nacl , lacl -> mapped_lun );
1109- if (!deve ) {
1110- rcu_read_unlock ();
1111- return - ENODEV ;
1112- }
1113-
1114- for_each_possible_cpu (cpu ) {
1115- stats = per_cpu_ptr (deve -> stats , cpu );
1116- bytes += stats -> write_bytes ;
1117- }
1118-
1119- /* scsiAuthIntrWrittenMegaBytes */
1120- ret = snprintf (page , PAGE_SIZE , "%u\n" , bytes >> 20 );
1121- rcu_read_unlock ();
1122- return ret ;
1123- }
998+ #define auth_show_per_cpu_stat (prefix , field , shift ) \
999+ per_cpu_stat_snprintf(se_dev_entry_io_stats, prefix, field, shift); \
1000+ static ssize_t \
1001+ target_stat_##prefix##_show(struct config_item *item, char *page) \
1002+ { \
1003+ struct se_lun_acl *lacl = auth_to_lacl(item); \
1004+ struct se_node_acl *nacl = lacl->se_lun_nacl; \
1005+ struct se_dev_entry *deve; \
1006+ int ret; \
1007+ \
1008+ rcu_read_lock(); \
1009+ deve = target_nacl_find_deve(nacl, lacl->mapped_lun); \
1010+ if (!deve) { \
1011+ rcu_read_unlock(); \
1012+ return -ENODEV; \
1013+ } \
1014+ \
1015+ ret = per_cpu_stat_##prefix##_snprintf(deve->stats, page); \
1016+ rcu_read_unlock(); \
1017+ return ret; \
1018+ }
1019+
1020+ /* scsiAuthIntrOutCommands */
1021+ auth_show_per_cpu_stat (auth_num_cmds , total_cmds , 0 );
1022+ /* scsiAuthIntrReadMegaBytes */
1023+ auth_show_per_cpu_stat (auth_read_mbytes , read_bytes , 20 );
1024+ /* scsiAuthIntrWrittenMegaBytes */
1025+ auth_show_per_cpu_stat (auth_write_mbytes , write_bytes , 20 );
11241026
11251027static ssize_t target_stat_auth_hs_num_cmds_show (struct config_item * item ,
11261028 char * page )
0 commit comments