22
22
#include <IOKit/ps/IOPSKeys.h>
23
23
#undef Size
24
24
25
+ #if (MAC_OS_X_VERSION_MAX_ALLOWED < 120000 ) // Before macOS 12 Monterey
26
+ #define kIOMainPortDefault kIOMasterPortDefault
27
+ #endif
28
+
25
29
/* Function used to get IO statistics of block devices */
26
30
void ReadIOAnalysisInformation (Tuplestorestate * tupstore , TupleDesc tupdesc )
27
31
{
@@ -37,7 +41,7 @@ void ReadIOAnalysisInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
37
41
memset (nulls , 0 , sizeof (nulls ));
38
42
39
43
// Get list of disks
40
- if (IOServiceGetMatchingServices (kIOMasterPortDefault ,
44
+ if (IOServiceGetMatchingServices (kIOMainPortDefault ,
41
45
IOServiceMatching (kIOMediaClass ),
42
46
& disk_list_iter ) != kIOReturnSuccess )
43
47
{
@@ -63,6 +67,17 @@ void ReadIOAnalysisInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
63
67
64
68
if (IOObjectConformsTo (parent_reg_ent , "IOBlockStorageDriver" ))
65
69
{
70
+ const int kMaxDiskNameSize = 64 ;
71
+ char device_name [kMaxDiskNameSize ];
72
+ CFStringRef disk_name_ref ;
73
+ CFNumberRef number ;
74
+ int64_t reads = 0 ;
75
+ int64_t writes = 0 ;
76
+ int64_t read_bytes = 0 ;
77
+ int64_t write_bytes = 0 ;
78
+ int64_t read_time_ns = 0 ;
79
+ int64_t write_time_ns = 0 ;
80
+
66
81
if (IORegistryEntryCreateCFProperties (
67
82
disk_reg_ent ,
68
83
(CFMutableDictionaryRef * ) & disk_parent_dict ,
@@ -89,9 +104,7 @@ void ReadIOAnalysisInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
89
104
return ;
90
105
}
91
106
92
- const int kMaxDiskNameSize = 64 ;
93
- CFStringRef disk_name_ref = (CFStringRef )CFDictionaryGetValue (disk_parent_dict , CFSTR (kIOBSDNameKey ));
94
- char device_name [kMaxDiskNameSize ];
107
+ disk_name_ref = (CFStringRef )CFDictionaryGetValue (disk_parent_dict , CFSTR (kIOBSDNameKey ));
95
108
96
109
CFStringGetCString (disk_name_ref ,
97
110
device_name ,
@@ -112,14 +125,6 @@ void ReadIOAnalysisInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
112
125
return ;
113
126
}
114
127
115
- CFNumberRef number ;
116
- int64_t reads = 0 ;
117
- int64_t writes = 0 ;
118
- int64_t read_bytes = 0 ;
119
- int64_t write_bytes = 0 ;
120
- int64_t read_time_ns = 0 ;
121
- int64_t write_time_ns = 0 ;
122
-
123
128
// Get disk reads/writes
124
129
if ((number = (CFNumberRef )CFDictionaryGetValue (
125
130
disk_stats_dict ,
@@ -167,12 +172,12 @@ void ReadIOAnalysisInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
167
172
write_time_ns = (int64_t )round (write_time_ns /1000000 );
168
173
169
174
values [Anum_device_name ] = CStringGetTextDatum (device_name );
170
- values [Anum_total_read ] = Int64GetDatumFast (reads );
171
- values [Anum_total_write ] = Int64GetDatumFast (writes );
172
- values [Anum_read_bytes ] = Int64GetDatumFast (read_bytes );
173
- values [Anum_write_bytes ] = Int64GetDatumFast (write_bytes );
174
- values [Anum_read_time_ms ] = Int64GetDatumFast (read_time_ns );
175
- values [Anum_write_time_ms ] = Int64GetDatumFast (write_time_ns );
175
+ values [Anum_total_read ] = UInt64GetDatum (reads );
176
+ values [Anum_total_write ] = UInt64GetDatum (writes );
177
+ values [Anum_read_bytes ] = UInt64GetDatum (read_bytes );
178
+ values [Anum_write_bytes ] = UInt64GetDatum (write_bytes );
179
+ values [Anum_read_time_ms ] = UInt64GetDatum (read_time_ns );
180
+ values [Anum_write_time_ms ] = UInt64GetDatum (write_time_ns );
176
181
177
182
tuplestore_putvalues (tupstore , tupdesc , values , nulls );
178
183
0 commit comments