mountstats.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Copyright 2018 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 procfs
  14. // While implementing parsing of /proc/[pid]/mountstats, this blog was used
  15. // heavily as a reference:
  16. // https://utcc.utoronto.ca/~cks/space/blog/linux/NFSMountstatsIndex
  17. //
  18. // Special thanks to Chris Siebenmann for all of his posts explaining the
  19. // various statistics available for NFS.
  20. import (
  21. "bufio"
  22. "fmt"
  23. "io"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. // Constants shared between multiple functions.
  29. const (
  30. deviceEntryLen = 8
  31. fieldBytesLen = 8
  32. fieldEventsLen = 27
  33. statVersion10 = "1.0"
  34. statVersion11 = "1.1"
  35. fieldTransport10Len = 10
  36. fieldTransport11Len = 13
  37. )
  38. // A Mount is a device mount parsed from /proc/[pid]/mountstats.
  39. type Mount struct {
  40. // Name of the device.
  41. Device string
  42. // The mount point of the device.
  43. Mount string
  44. // The filesystem type used by the device.
  45. Type string
  46. // If available additional statistics related to this Mount.
  47. // Use a type assertion to determine if additional statistics are available.
  48. Stats MountStats
  49. }
  50. // A MountStats is a type which contains detailed statistics for a specific
  51. // type of Mount.
  52. type MountStats interface {
  53. mountStats()
  54. }
  55. // A MountStatsNFS is a MountStats implementation for NFSv3 and v4 mounts.
  56. type MountStatsNFS struct {
  57. // The version of statistics provided.
  58. StatVersion string
  59. // The age of the NFS mount.
  60. Age time.Duration
  61. // Statistics related to byte counters for various operations.
  62. Bytes NFSBytesStats
  63. // Statistics related to various NFS event occurrences.
  64. Events NFSEventsStats
  65. // Statistics broken down by filesystem operation.
  66. Operations []NFSOperationStats
  67. // Statistics about the NFS RPC transport.
  68. Transport NFSTransportStats
  69. }
  70. // mountStats implements MountStats.
  71. func (m MountStatsNFS) mountStats() {}
  72. // A NFSBytesStats contains statistics about the number of bytes read and written
  73. // by an NFS client to and from an NFS server.
  74. type NFSBytesStats struct {
  75. // Number of bytes read using the read() syscall.
  76. Read uint64
  77. // Number of bytes written using the write() syscall.
  78. Write uint64
  79. // Number of bytes read using the read() syscall in O_DIRECT mode.
  80. DirectRead uint64
  81. // Number of bytes written using the write() syscall in O_DIRECT mode.
  82. DirectWrite uint64
  83. // Number of bytes read from the NFS server, in total.
  84. ReadTotal uint64
  85. // Number of bytes written to the NFS server, in total.
  86. WriteTotal uint64
  87. // Number of pages read directly via mmap()'d files.
  88. ReadPages uint64
  89. // Number of pages written directly via mmap()'d files.
  90. WritePages uint64
  91. }
  92. // A NFSEventsStats contains statistics about NFS event occurrences.
  93. type NFSEventsStats struct {
  94. // Number of times cached inode attributes are re-validated from the server.
  95. InodeRevalidate uint64
  96. // Number of times cached dentry nodes are re-validated from the server.
  97. DnodeRevalidate uint64
  98. // Number of times an inode cache is cleared.
  99. DataInvalidate uint64
  100. // Number of times cached inode attributes are invalidated.
  101. AttributeInvalidate uint64
  102. // Number of times files or directories have been open()'d.
  103. VFSOpen uint64
  104. // Number of times a directory lookup has occurred.
  105. VFSLookup uint64
  106. // Number of times permissions have been checked.
  107. VFSAccess uint64
  108. // Number of updates (and potential writes) to pages.
  109. VFSUpdatePage uint64
  110. // Number of pages read directly via mmap()'d files.
  111. VFSReadPage uint64
  112. // Number of times a group of pages have been read.
  113. VFSReadPages uint64
  114. // Number of pages written directly via mmap()'d files.
  115. VFSWritePage uint64
  116. // Number of times a group of pages have been written.
  117. VFSWritePages uint64
  118. // Number of times directory entries have been read with getdents().
  119. VFSGetdents uint64
  120. // Number of times attributes have been set on inodes.
  121. VFSSetattr uint64
  122. // Number of pending writes that have been forcefully flushed to the server.
  123. VFSFlush uint64
  124. // Number of times fsync() has been called on directories and files.
  125. VFSFsync uint64
  126. // Number of times locking has been attempted on a file.
  127. VFSLock uint64
  128. // Number of times files have been closed and released.
  129. VFSFileRelease uint64
  130. // Unknown. Possibly unused.
  131. CongestionWait uint64
  132. // Number of times files have been truncated.
  133. Truncation uint64
  134. // Number of times a file has been grown due to writes beyond its existing end.
  135. WriteExtension uint64
  136. // Number of times a file was removed while still open by another process.
  137. SillyRename uint64
  138. // Number of times the NFS server gave less data than expected while reading.
  139. ShortRead uint64
  140. // Number of times the NFS server wrote less data than expected while writing.
  141. ShortWrite uint64
  142. // Number of times the NFS server indicated EJUKEBOX; retrieving data from
  143. // offline storage.
  144. JukeboxDelay uint64
  145. // Number of NFS v4.1+ pNFS reads.
  146. PNFSRead uint64
  147. // Number of NFS v4.1+ pNFS writes.
  148. PNFSWrite uint64
  149. }
  150. // A NFSOperationStats contains statistics for a single operation.
  151. type NFSOperationStats struct {
  152. // The name of the operation.
  153. Operation string
  154. // Number of requests performed for this operation.
  155. Requests uint64
  156. // Number of times an actual RPC request has been transmitted for this operation.
  157. Transmissions uint64
  158. // Number of times a request has had a major timeout.
  159. MajorTimeouts uint64
  160. // Number of bytes sent for this operation, including RPC headers and payload.
  161. BytesSent uint64
  162. // Number of bytes received for this operation, including RPC headers and payload.
  163. BytesReceived uint64
  164. // Duration all requests spent queued for transmission before they were sent.
  165. CumulativeQueueTime time.Duration
  166. // Duration it took to get a reply back after the request was transmitted.
  167. CumulativeTotalResponseTime time.Duration
  168. // Duration from when a request was enqueued to when it was completely handled.
  169. CumulativeTotalRequestTime time.Duration
  170. }
  171. // A NFSTransportStats contains statistics for the NFS mount RPC requests and
  172. // responses.
  173. type NFSTransportStats struct {
  174. // The local port used for the NFS mount.
  175. Port uint64
  176. // Number of times the client has had to establish a connection from scratch
  177. // to the NFS server.
  178. Bind uint64
  179. // Number of times the client has made a TCP connection to the NFS server.
  180. Connect uint64
  181. // Duration (in jiffies, a kernel internal unit of time) the NFS mount has
  182. // spent waiting for connections to the server to be established.
  183. ConnectIdleTime uint64
  184. // Duration since the NFS mount last saw any RPC traffic.
  185. IdleTime time.Duration
  186. // Number of RPC requests for this mount sent to the NFS server.
  187. Sends uint64
  188. // Number of RPC responses for this mount received from the NFS server.
  189. Receives uint64
  190. // Number of times the NFS server sent a response with a transaction ID
  191. // unknown to this client.
  192. BadTransactionIDs uint64
  193. // A running counter, incremented on each request as the current difference
  194. // ebetween sends and receives.
  195. CumulativeActiveRequests uint64
  196. // A running counter, incremented on each request by the current backlog
  197. // queue size.
  198. CumulativeBacklog uint64
  199. // Stats below only available with stat version 1.1.
  200. // Maximum number of simultaneously active RPC requests ever used.
  201. MaximumRPCSlotsUsed uint64
  202. // A running counter, incremented on each request as the current size of the
  203. // sending queue.
  204. CumulativeSendingQueue uint64
  205. // A running counter, incremented on each request as the current size of the
  206. // pending queue.
  207. CumulativePendingQueue uint64
  208. }
  209. // parseMountStats parses a /proc/[pid]/mountstats file and returns a slice
  210. // of Mount structures containing detailed information about each mount.
  211. // If available, statistics for each mount are parsed as well.
  212. func parseMountStats(r io.Reader) ([]*Mount, error) {
  213. const (
  214. device = "device"
  215. statVersionPrefix = "statvers="
  216. nfs3Type = "nfs"
  217. nfs4Type = "nfs4"
  218. )
  219. var mounts []*Mount
  220. s := bufio.NewScanner(r)
  221. for s.Scan() {
  222. // Only look for device entries in this function
  223. ss := strings.Fields(string(s.Bytes()))
  224. if len(ss) == 0 || ss[0] != device {
  225. continue
  226. }
  227. m, err := parseMount(ss)
  228. if err != nil {
  229. return nil, err
  230. }
  231. // Does this mount also possess statistics information?
  232. if len(ss) > deviceEntryLen {
  233. // Only NFSv3 and v4 are supported for parsing statistics
  234. if m.Type != nfs3Type && m.Type != nfs4Type {
  235. return nil, fmt.Errorf("cannot parse MountStats for fstype %q", m.Type)
  236. }
  237. statVersion := strings.TrimPrefix(ss[8], statVersionPrefix)
  238. stats, err := parseMountStatsNFS(s, statVersion)
  239. if err != nil {
  240. return nil, err
  241. }
  242. m.Stats = stats
  243. }
  244. mounts = append(mounts, m)
  245. }
  246. return mounts, s.Err()
  247. }
  248. // parseMount parses an entry in /proc/[pid]/mountstats in the format:
  249. // device [device] mounted on [mount] with fstype [type]
  250. func parseMount(ss []string) (*Mount, error) {
  251. if len(ss) < deviceEntryLen {
  252. return nil, fmt.Errorf("invalid device entry: %v", ss)
  253. }
  254. // Check for specific words appearing at specific indices to ensure
  255. // the format is consistent with what we expect
  256. format := []struct {
  257. i int
  258. s string
  259. }{
  260. {i: 0, s: "device"},
  261. {i: 2, s: "mounted"},
  262. {i: 3, s: "on"},
  263. {i: 5, s: "with"},
  264. {i: 6, s: "fstype"},
  265. }
  266. for _, f := range format {
  267. if ss[f.i] != f.s {
  268. return nil, fmt.Errorf("invalid device entry: %v", ss)
  269. }
  270. }
  271. return &Mount{
  272. Device: ss[1],
  273. Mount: ss[4],
  274. Type: ss[7],
  275. }, nil
  276. }
  277. // parseMountStatsNFS parses a MountStatsNFS by scanning additional information
  278. // related to NFS statistics.
  279. func parseMountStatsNFS(s *bufio.Scanner, statVersion string) (*MountStatsNFS, error) {
  280. // Field indicators for parsing specific types of data
  281. const (
  282. fieldAge = "age:"
  283. fieldBytes = "bytes:"
  284. fieldEvents = "events:"
  285. fieldPerOpStats = "per-op"
  286. fieldTransport = "xprt:"
  287. )
  288. stats := &MountStatsNFS{
  289. StatVersion: statVersion,
  290. }
  291. for s.Scan() {
  292. ss := strings.Fields(string(s.Bytes()))
  293. if len(ss) == 0 {
  294. break
  295. }
  296. if len(ss) < 2 {
  297. return nil, fmt.Errorf("not enough information for NFS stats: %v", ss)
  298. }
  299. switch ss[0] {
  300. case fieldAge:
  301. // Age integer is in seconds
  302. d, err := time.ParseDuration(ss[1] + "s")
  303. if err != nil {
  304. return nil, err
  305. }
  306. stats.Age = d
  307. case fieldBytes:
  308. bstats, err := parseNFSBytesStats(ss[1:])
  309. if err != nil {
  310. return nil, err
  311. }
  312. stats.Bytes = *bstats
  313. case fieldEvents:
  314. estats, err := parseNFSEventsStats(ss[1:])
  315. if err != nil {
  316. return nil, err
  317. }
  318. stats.Events = *estats
  319. case fieldTransport:
  320. if len(ss) < 3 {
  321. return nil, fmt.Errorf("not enough information for NFS transport stats: %v", ss)
  322. }
  323. tstats, err := parseNFSTransportStats(ss[2:], statVersion)
  324. if err != nil {
  325. return nil, err
  326. }
  327. stats.Transport = *tstats
  328. }
  329. // When encountering "per-operation statistics", we must break this
  330. // loop and parse them separately to ensure we can terminate parsing
  331. // before reaching another device entry; hence why this 'if' statement
  332. // is not just another switch case
  333. if ss[0] == fieldPerOpStats {
  334. break
  335. }
  336. }
  337. if err := s.Err(); err != nil {
  338. return nil, err
  339. }
  340. // NFS per-operation stats appear last before the next device entry
  341. perOpStats, err := parseNFSOperationStats(s)
  342. if err != nil {
  343. return nil, err
  344. }
  345. stats.Operations = perOpStats
  346. return stats, nil
  347. }
  348. // parseNFSBytesStats parses a NFSBytesStats line using an input set of
  349. // integer fields.
  350. func parseNFSBytesStats(ss []string) (*NFSBytesStats, error) {
  351. if len(ss) != fieldBytesLen {
  352. return nil, fmt.Errorf("invalid NFS bytes stats: %v", ss)
  353. }
  354. ns := make([]uint64, 0, fieldBytesLen)
  355. for _, s := range ss {
  356. n, err := strconv.ParseUint(s, 10, 64)
  357. if err != nil {
  358. return nil, err
  359. }
  360. ns = append(ns, n)
  361. }
  362. return &NFSBytesStats{
  363. Read: ns[0],
  364. Write: ns[1],
  365. DirectRead: ns[2],
  366. DirectWrite: ns[3],
  367. ReadTotal: ns[4],
  368. WriteTotal: ns[5],
  369. ReadPages: ns[6],
  370. WritePages: ns[7],
  371. }, nil
  372. }
  373. // parseNFSEventsStats parses a NFSEventsStats line using an input set of
  374. // integer fields.
  375. func parseNFSEventsStats(ss []string) (*NFSEventsStats, error) {
  376. if len(ss) != fieldEventsLen {
  377. return nil, fmt.Errorf("invalid NFS events stats: %v", ss)
  378. }
  379. ns := make([]uint64, 0, fieldEventsLen)
  380. for _, s := range ss {
  381. n, err := strconv.ParseUint(s, 10, 64)
  382. if err != nil {
  383. return nil, err
  384. }
  385. ns = append(ns, n)
  386. }
  387. return &NFSEventsStats{
  388. InodeRevalidate: ns[0],
  389. DnodeRevalidate: ns[1],
  390. DataInvalidate: ns[2],
  391. AttributeInvalidate: ns[3],
  392. VFSOpen: ns[4],
  393. VFSLookup: ns[5],
  394. VFSAccess: ns[6],
  395. VFSUpdatePage: ns[7],
  396. VFSReadPage: ns[8],
  397. VFSReadPages: ns[9],
  398. VFSWritePage: ns[10],
  399. VFSWritePages: ns[11],
  400. VFSGetdents: ns[12],
  401. VFSSetattr: ns[13],
  402. VFSFlush: ns[14],
  403. VFSFsync: ns[15],
  404. VFSLock: ns[16],
  405. VFSFileRelease: ns[17],
  406. CongestionWait: ns[18],
  407. Truncation: ns[19],
  408. WriteExtension: ns[20],
  409. SillyRename: ns[21],
  410. ShortRead: ns[22],
  411. ShortWrite: ns[23],
  412. JukeboxDelay: ns[24],
  413. PNFSRead: ns[25],
  414. PNFSWrite: ns[26],
  415. }, nil
  416. }
  417. // parseNFSOperationStats parses a slice of NFSOperationStats by scanning
  418. // additional information about per-operation statistics until an empty
  419. // line is reached.
  420. func parseNFSOperationStats(s *bufio.Scanner) ([]NFSOperationStats, error) {
  421. const (
  422. // Number of expected fields in each per-operation statistics set
  423. numFields = 9
  424. )
  425. var ops []NFSOperationStats
  426. for s.Scan() {
  427. ss := strings.Fields(string(s.Bytes()))
  428. if len(ss) == 0 {
  429. // Must break when reading a blank line after per-operation stats to
  430. // enable top-level function to parse the next device entry
  431. break
  432. }
  433. if len(ss) != numFields {
  434. return nil, fmt.Errorf("invalid NFS per-operations stats: %v", ss)
  435. }
  436. // Skip string operation name for integers
  437. ns := make([]uint64, 0, numFields-1)
  438. for _, st := range ss[1:] {
  439. n, err := strconv.ParseUint(st, 10, 64)
  440. if err != nil {
  441. return nil, err
  442. }
  443. ns = append(ns, n)
  444. }
  445. ops = append(ops, NFSOperationStats{
  446. Operation: strings.TrimSuffix(ss[0], ":"),
  447. Requests: ns[0],
  448. Transmissions: ns[1],
  449. MajorTimeouts: ns[2],
  450. BytesSent: ns[3],
  451. BytesReceived: ns[4],
  452. CumulativeQueueTime: time.Duration(ns[5]) * time.Millisecond,
  453. CumulativeTotalResponseTime: time.Duration(ns[6]) * time.Millisecond,
  454. CumulativeTotalRequestTime: time.Duration(ns[7]) * time.Millisecond,
  455. })
  456. }
  457. return ops, s.Err()
  458. }
  459. // parseNFSTransportStats parses a NFSTransportStats line using an input set of
  460. // integer fields matched to a specific stats version.
  461. func parseNFSTransportStats(ss []string, statVersion string) (*NFSTransportStats, error) {
  462. switch statVersion {
  463. case statVersion10:
  464. if len(ss) != fieldTransport10Len {
  465. return nil, fmt.Errorf("invalid NFS transport stats 1.0 statement: %v", ss)
  466. }
  467. case statVersion11:
  468. if len(ss) != fieldTransport11Len {
  469. return nil, fmt.Errorf("invalid NFS transport stats 1.1 statement: %v", ss)
  470. }
  471. default:
  472. return nil, fmt.Errorf("unrecognized NFS transport stats version: %q", statVersion)
  473. }
  474. // Allocate enough for v1.1 stats since zero value for v1.1 stats will be okay
  475. // in a v1.0 response.
  476. //
  477. // Note: slice length must be set to length of v1.1 stats to avoid a panic when
  478. // only v1.0 stats are present.
  479. // See: https://github.com/prometheus/node_exporter/issues/571.
  480. ns := make([]uint64, fieldTransport11Len)
  481. for i, s := range ss {
  482. n, err := strconv.ParseUint(s, 10, 64)
  483. if err != nil {
  484. return nil, err
  485. }
  486. ns[i] = n
  487. }
  488. return &NFSTransportStats{
  489. Port: ns[0],
  490. Bind: ns[1],
  491. Connect: ns[2],
  492. ConnectIdleTime: ns[3],
  493. IdleTime: time.Duration(ns[4]) * time.Second,
  494. Sends: ns[5],
  495. Receives: ns[6],
  496. BadTransactionIDs: ns[7],
  497. CumulativeActiveRequests: ns[8],
  498. CumulativeBacklog: ns[9],
  499. MaximumRPCSlotsUsed: ns[10],
  500. CumulativeSendingQueue: ns[11],
  501. CumulativePendingQueue: ns[12],
  502. }, nil
  503. }