Skip to content

Commit c130735

Browse files
committed
Cleaned up impl per core algorithm changes, defined in HLD
1 parent 2890fba commit c130735

File tree

1 file changed

+14
-81
lines changed

1 file changed

+14
-81
lines changed

sonic_platform_base/sonic_storage/storage_common.py

+14-81
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
try:
99
import os
1010
import sys
11+
import json
1112
except ImportError as e:
1213
raise ImportError (str(e) + "- required module not found")
1314

@@ -16,6 +17,7 @@
1617
except ImportError as e:
1718
pass
1819

20+
1921
class StorageCommon(object):
2022
def __init__(self, diskdev):
2123
"""
@@ -25,121 +27,52 @@ def __init__(self, diskdev):
2527
Block device path for which we need to get information
2628
"""
2729
self.DISKSTATS_FILE = "/proc/diskstats"
28-
self.MOUNTS_FILE = "/proc/mounts"
29-
self.FSSTATS_FILE_PREFIX = "/host/storagemon/fs-stats"
3030
self.diskdev = os.path.basename(diskdev)
31-
self.fs_reads = 0
32-
self.fs_writes = 0
33-
self._parse_fsstats_file()
34-
35-
def _parse_fsstats_file(self):
36-
"""
37-
Function to parse a file containing the previous latest FS IO Reads/Writes values from the corresponding disk's file and saves it to member variables
38-
39-
Args: None
40-
41-
Returns: None
42-
43-
"""
44-
filename = self.FSSTATS_FILE_PREFIX + "-" + self.diskdev
45-
46-
if not os.path.isfile(filename):
47-
with open(filename, 'w') as f:
48-
f.write("{} {}".format(self.fs_reads, self.fs_writes))
49-
return
50-
else:
51-
try:
52-
with open(filename) as f:
53-
fsstats = f.readline().strip()
54-
self.fs_reads = fsstats.split()[0]
55-
self.fs_writes = fsstats.split()[1]
56-
except Exception as e:
57-
os.remove(filename)
58-
pass
59-
60-
61-
def _update_fsstats_file(self, value, attr):
62-
"""
63-
Function to update the latest FS IO Reads/Writes (fs_reads/writes + crresponding value parsed from /proc/diskstats) to the disk's fsstats file
64-
65-
Args: value, 'R' or 'W' to indicate which field to update in the file
66-
67-
Returns:
68-
N/A
69-
"""
70-
filename = self.FSSTATS_FILE_PREFIX + "-" + self.diskdev
71-
fsstats = ""
72-
73-
if not os.path.isfile(filename):
74-
if attr == 'R':
75-
fsstats = ("{} {}".format(value, self.fs_writes))
76-
elif attr == 'W':
77-
fsstats = ("{} {}".format(self.fs_reads, value))
78-
else:
79-
with open(filename) as f:
80-
fsstats = f.readline().strip()
81-
82-
if attr == 'R':
83-
fsstats = "{} {}".format(value, fsstats.split()[1])
84-
elif attr == 'W':
85-
fsstats = "{} {}".format(fsstats.split()[0], value)
86-
87-
with open(filename, 'w') as f:
88-
f.write(fsstats)
31+
self.fsstats_reads = 0
32+
self.fsstats_writes = 0
8933

9034
def get_fs_io_reads(self):
9135
"""
92-
Function to get the total number of reads on the disk by parsing the /proc/diskstats file
36+
Function to get the latest reads on the disk by parsing the /proc/diskstats file
9337
9438
Returns:
95-
The total number of disk reads
39+
The total number of procfs reads
9640
9741
Args:
9842
N/A
9943
"""
10044

101-
reads = 0
102-
10345
if 'psutil' in sys.modules:
104-
reads = int(psutil.disk_io_counters(perdisk=True, nowrap=True)[self.diskdev].read_count)
46+
self.fsstats_reads = int(psutil.disk_io_counters(perdisk=True, nowrap=True)[self.diskdev].read_count)
10547
else:
10648
with open(self.DISKSTATS_FILE) as f:
10749
statsfile = f.readlines()
10850
for line in statsfile:
10951
if self.diskdev == line.split()[2]:
110-
reads = int(line.split()[3])
52+
self.fsstats_reads = int(line.split()[3])
11153
break
11254

113-
total_reads = int(self.fs_reads) + reads
114-
self._update_fsstats_file(total_reads, 'R')
115-
116-
return total_reads
117-
55+
return self.fsstats_reads
11856

11957
def get_fs_io_writes(self):
12058
"""
121-
Function to get the total number of disk writes by parsing the /proc/diskstats file
59+
Function to get the latest disk writes by parsing the /proc/diskstats file
12260
12361
Returns:
124-
The total number of disk writes
62+
The total number of procfs writes
12563
12664
Args:
12765
N/A
12866
"""
12967

130-
writes = 0
131-
13268
if 'psutil' in sys.modules:
133-
writes = psutil.disk_io_counters(perdisk=True, nowrap=True)[self.diskdev].write_count
69+
self.fsstats_writes = psutil.disk_io_counters(perdisk=True, nowrap=True)[self.diskdev].write_count
13470
else:
13571
with open(self.DISKSTATS_FILE) as f:
13672
statsfile = f.readlines()
13773
for line in statsfile:
13874
if self.diskdev == line.split()[2]:
139-
writes = int(line.split()[7])
75+
self.fsstats_writes = int(line.split()[7])
14076
break
14177

142-
total_writes = int(self.fs_writes) + writes
143-
self._update_fsstats_file(total_writes, 'W')
144-
145-
return total_writes
78+
return self.fsstats_writes

0 commit comments

Comments
 (0)