xfs.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Copyright 2017 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Package xfs provides access to statistics exposed by the XFS filesystem.
  14. package xfs
  15. import (
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "github.com/prometheus/procfs/internal/fs"
  20. )
  21. // Stats contains XFS filesystem runtime statistics, parsed from
  22. // /proc/fs/xfs/stat.
  23. //
  24. // The names and meanings of each statistic were taken from
  25. // http://xfs.org/index.php/Runtime_Stats and xfs_stats.h in the Linux
  26. // kernel source. Most counters are uint32s (same data types used in
  27. // xfs_stats.h), but some of the "extended precision stats" are uint64s.
  28. type Stats struct {
  29. // The name of the filesystem used to source these statistics.
  30. // If empty, this indicates aggregated statistics for all XFS
  31. // filesystems on the host.
  32. Name string
  33. ExtentAllocation ExtentAllocationStats
  34. AllocationBTree BTreeStats
  35. BlockMapping BlockMappingStats
  36. BlockMapBTree BTreeStats
  37. DirectoryOperation DirectoryOperationStats
  38. Transaction TransactionStats
  39. InodeOperation InodeOperationStats
  40. LogOperation LogOperationStats
  41. ReadWrite ReadWriteStats
  42. AttributeOperation AttributeOperationStats
  43. InodeClustering InodeClusteringStats
  44. Vnode VnodeStats
  45. Buffer BufferStats
  46. ExtendedPrecision ExtendedPrecisionStats
  47. Xstrat XstratStats // xstrat
  48. PushAil PushAilStats // push_ail
  49. Debug DebugStats // debug
  50. QuotaManager QuotaManagerStats // qm
  51. BtreeAllocBlocks2 BtreeAllocBlocks2Stats // abtb2
  52. BtreeAllocContig2 BtreeAllocContig2Stats // abtc2
  53. BtreeBlockMap2 BtreeBlockMap2Stats // bmbt2
  54. BtreeInode2 BtreeInode2Stats // ibt2
  55. }
  56. // ExtentAllocationStats contains statistics regarding XFS extent allocations.
  57. type ExtentAllocationStats struct {
  58. ExtentsAllocated uint32
  59. BlocksAllocated uint32
  60. ExtentsFreed uint32
  61. BlocksFreed uint32
  62. }
  63. // BTreeStats contains statistics regarding an XFS internal B-tree.
  64. type BTreeStats struct {
  65. Lookups uint32
  66. Compares uint32
  67. RecordsInserted uint32
  68. RecordsDeleted uint32
  69. }
  70. // BlockMappingStats contains statistics regarding XFS block maps.
  71. type BlockMappingStats struct {
  72. Reads uint32
  73. Writes uint32
  74. Unmaps uint32
  75. ExtentListInsertions uint32
  76. ExtentListDeletions uint32
  77. ExtentListLookups uint32
  78. ExtentListCompares uint32
  79. }
  80. // DirectoryOperationStats contains statistics regarding XFS directory entries.
  81. type DirectoryOperationStats struct {
  82. Lookups uint32
  83. Creates uint32
  84. Removes uint32
  85. Getdents uint32
  86. }
  87. // TransactionStats contains statistics regarding XFS metadata transactions.
  88. type TransactionStats struct {
  89. Sync uint32
  90. Async uint32
  91. Empty uint32
  92. }
  93. // InodeOperationStats contains statistics regarding XFS inode operations.
  94. type InodeOperationStats struct {
  95. Attempts uint32
  96. Found uint32
  97. Recycle uint32
  98. Missed uint32
  99. Duplicate uint32
  100. Reclaims uint32
  101. AttributeChange uint32
  102. }
  103. // LogOperationStats contains statistics regarding the XFS log buffer.
  104. type LogOperationStats struct {
  105. Writes uint32
  106. Blocks uint32
  107. NoInternalBuffers uint32
  108. Force uint32
  109. ForceSleep uint32
  110. }
  111. // ReadWriteStats contains statistics regarding the number of read and write
  112. // system calls for XFS filesystems.
  113. type ReadWriteStats struct {
  114. Write uint32
  115. Read uint32
  116. }
  117. // AttributeOperationStats contains statistics regarding manipulation of
  118. // XFS extended file attributes.
  119. type AttributeOperationStats struct {
  120. Get uint32
  121. Set uint32
  122. Remove uint32
  123. List uint32
  124. }
  125. // InodeClusteringStats contains statistics regarding XFS inode clustering
  126. // operations.
  127. type InodeClusteringStats struct {
  128. Iflush uint32
  129. Flush uint32
  130. FlushInode uint32
  131. }
  132. // VnodeStats contains statistics regarding XFS vnode operations.
  133. type VnodeStats struct {
  134. Active uint32
  135. Allocate uint32
  136. Get uint32
  137. Hold uint32
  138. Release uint32
  139. Reclaim uint32
  140. Remove uint32
  141. Free uint32
  142. }
  143. // BufferStats contains statistics regarding XFS read/write I/O buffers.
  144. type BufferStats struct {
  145. Get uint32
  146. Create uint32
  147. GetLocked uint32
  148. GetLockedWaited uint32
  149. BusyLocked uint32
  150. MissLocked uint32
  151. PageRetries uint32
  152. PageFound uint32
  153. GetRead uint32
  154. }
  155. // ExtendedPrecisionStats contains high precision counters used to track the
  156. // total number of bytes read, written, or flushed, during XFS operations.
  157. type ExtendedPrecisionStats struct {
  158. FlushBytes uint64
  159. WriteBytes uint64
  160. ReadBytes uint64
  161. }
  162. // PushAilStats contains statistics on tail-pushing operations.
  163. type PushAilStats struct {
  164. TryLogspace uint32
  165. SleepLogspace uint32
  166. Pushes uint32
  167. Success uint32
  168. PushBuf uint32
  169. Pinned uint32
  170. Locked uint32
  171. Flushing uint32
  172. Restarts uint32
  173. Flush uint32
  174. }
  175. // QuotaManagerStats contain statistics regarding quota processing.
  176. type QuotaManagerStats struct {
  177. Reclaims uint32
  178. ReclaimMisses uint32
  179. DquoteDups uint32
  180. CacheMisses uint32
  181. CacheHits uint32
  182. Wants uint32
  183. ShakeReclaims uint32
  184. InactReclaims uint32
  185. Unused uint32
  186. }
  187. // XstratStats contains statistics regarding bytes processed by the XFS daemon.
  188. type XstratStats struct {
  189. Quick uint32
  190. Split uint32
  191. }
  192. // DebugStats indicate if XFS debugging is enabled.
  193. type DebugStats struct {
  194. Enabled uint32
  195. }
  196. // BtreeAllocBlocks2Stats contains statistics on B-Tree v2 allocations.
  197. type BtreeAllocBlocks2Stats struct {
  198. Lookup uint32
  199. Compare uint32
  200. Insrec uint32
  201. Delrec uint32
  202. NewRoot uint32
  203. KillRoot uint32
  204. Increment uint32
  205. Decrement uint32
  206. Lshift uint32
  207. Rshift uint32
  208. Split uint32
  209. Join uint32
  210. Alloc uint32
  211. Free uint32
  212. Moves uint32
  213. }
  214. // BtreeAllocContig2Stats contain statistics on B-tree v2 free-space-by-size record operations.
  215. type BtreeAllocContig2Stats struct {
  216. Lookup uint32
  217. Compare uint32
  218. Insrec uint32
  219. Delrec uint32
  220. NewRoot uint32
  221. KillRoot uint32
  222. Increment uint32
  223. Decrement uint32
  224. Lshift uint32
  225. Rshift uint32
  226. Split uint32
  227. Join uint32
  228. Alloc uint32
  229. Free uint32
  230. Moves uint32
  231. }
  232. // BtreeBlockMap2Stats contain statistics on B-tree v2 block map operations.
  233. type BtreeBlockMap2Stats struct {
  234. Lookup uint32
  235. Compare uint32
  236. Insrec uint32
  237. Delrec uint32
  238. NewRoot uint32
  239. KillRoot uint32
  240. Increment uint32
  241. Decrement uint32
  242. Lshift uint32
  243. Rshift uint32
  244. Split uint32
  245. Join uint32
  246. Alloc uint32
  247. Free uint32
  248. Moves uint32
  249. }
  250. // BtreeInode2Stats contain statistics on B-tree v2 inode allocations.
  251. type BtreeInode2Stats struct {
  252. Lookup uint32
  253. Compare uint32
  254. Insrec uint32
  255. Delrec uint32
  256. NewRoot uint32
  257. KillRoot uint32
  258. Increment uint32
  259. Decrement uint32
  260. Lshift uint32
  261. Rshift uint32
  262. Split uint32
  263. Join uint32
  264. Alloc uint32
  265. Free uint32
  266. Moves uint32
  267. }
  268. // FS represents the pseudo-filesystems proc and sys, which provides an interface to
  269. // kernel data structures.
  270. type FS struct {
  271. proc *fs.FS
  272. sys *fs.FS
  273. }
  274. // NewDefaultFS returns a new XFS handle using the default proc and sys mountPoints.
  275. // It will error if either of the mounts point can't be read.
  276. func NewDefaultFS() (FS, error) {
  277. return NewFS(fs.DefaultProcMountPoint, fs.DefaultSysMountPoint)
  278. }
  279. // NewFS returns a new XFS handle using the given proc and sys mountPoints. It will error
  280. // if either of the mounts point can't be read.
  281. func NewFS(procMountPoint string, sysMountPoint string) (FS, error) {
  282. if strings.TrimSpace(procMountPoint) == "" {
  283. procMountPoint = fs.DefaultProcMountPoint
  284. }
  285. procfs, err := fs.NewFS(procMountPoint)
  286. if err != nil {
  287. return FS{}, err
  288. }
  289. if strings.TrimSpace(sysMountPoint) == "" {
  290. sysMountPoint = fs.DefaultSysMountPoint
  291. }
  292. sysfs, err := fs.NewFS(sysMountPoint)
  293. if err != nil {
  294. return FS{}, err
  295. }
  296. return FS{&procfs, &sysfs}, nil
  297. }
  298. // ProcStat retrieves XFS filesystem runtime statistics
  299. // from proc/fs/xfs/stat given the profs mount point.
  300. func (fs FS) ProcStat() (*Stats, error) {
  301. f, err := os.Open(fs.proc.Path("fs/xfs/stat"))
  302. if err != nil {
  303. return nil, err
  304. }
  305. defer f.Close()
  306. return ParseStats(f)
  307. }
  308. // SysStats retrieves XFS filesystem runtime statistics for each mounted XFS
  309. // filesystem. Only available on kernel 4.4+. On older kernels, an empty
  310. // slice of *xfs.Stats will be returned.
  311. func (fs FS) SysStats() ([]*Stats, error) {
  312. matches, err := filepath.Glob(fs.sys.Path("fs/xfs/*/stats/stats"))
  313. if err != nil {
  314. return nil, err
  315. }
  316. stats := make([]*Stats, 0, len(matches))
  317. for _, m := range matches {
  318. f, err := os.Open(m)
  319. if err != nil {
  320. return nil, err
  321. }
  322. // "*" used in glob above indicates the name of the filesystem.
  323. name := filepath.Base(filepath.Dir(filepath.Dir(m)))
  324. // File must be closed after parsing, regardless of success or
  325. // failure. Defer is not used because of the loop.
  326. s, err := ParseStats(f)
  327. _ = f.Close()
  328. if err != nil {
  329. return nil, err
  330. }
  331. s.Name = name
  332. stats = append(stats, s)
  333. }
  334. return stats, nil
  335. }