mountstats.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. fieldTransport10TCPLen = 10
  36. fieldTransport10UDPLen = 7
  37. fieldTransport11TCPLen = 13
  38. fieldTransport11UDPLen = 10
  39. // kernel version >= 4.14 MaxLen
  40. // See: https://elixir.bootlin.com/linux/v6.4.8/source/net/sunrpc/xprtrdma/xprt_rdma.h#L393
  41. fieldTransport11RDMAMaxLen = 28
  42. // kernel version <= 4.2 MinLen
  43. // See: https://elixir.bootlin.com/linux/v4.2.8/source/net/sunrpc/xprtrdma/xprt_rdma.h#L331
  44. fieldTransport11RDMAMinLen = 20
  45. )
  46. // A Mount is a device mount parsed from /proc/[pid]/mountstats.
  47. type Mount struct {
  48. // Name of the device.
  49. Device string
  50. // The mount point of the device.
  51. Mount string
  52. // The filesystem type used by the device.
  53. Type string
  54. // If available additional statistics related to this Mount.
  55. // Use a type assertion to determine if additional statistics are available.
  56. Stats MountStats
  57. }
  58. // A MountStats is a type which contains detailed statistics for a specific
  59. // type of Mount.
  60. type MountStats interface {
  61. mountStats()
  62. }
  63. // A MountStatsNFS is a MountStats implementation for NFSv3 and v4 mounts.
  64. type MountStatsNFS struct {
  65. // The version of statistics provided.
  66. StatVersion string
  67. // The mount options of the NFS mount.
  68. Opts map[string]string
  69. // The age of the NFS mount.
  70. Age time.Duration
  71. // Statistics related to byte counters for various operations.
  72. Bytes NFSBytesStats
  73. // Statistics related to various NFS event occurrences.
  74. Events NFSEventsStats
  75. // Statistics broken down by filesystem operation.
  76. Operations []NFSOperationStats
  77. // Statistics about the NFS RPC transport.
  78. Transport NFSTransportStats
  79. }
  80. // mountStats implements MountStats.
  81. func (m MountStatsNFS) mountStats() {}
  82. // A NFSBytesStats contains statistics about the number of bytes read and written
  83. // by an NFS client to and from an NFS server.
  84. type NFSBytesStats struct {
  85. // Number of bytes read using the read() syscall.
  86. Read uint64
  87. // Number of bytes written using the write() syscall.
  88. Write uint64
  89. // Number of bytes read using the read() syscall in O_DIRECT mode.
  90. DirectRead uint64
  91. // Number of bytes written using the write() syscall in O_DIRECT mode.
  92. DirectWrite uint64
  93. // Number of bytes read from the NFS server, in total.
  94. ReadTotal uint64
  95. // Number of bytes written to the NFS server, in total.
  96. WriteTotal uint64
  97. // Number of pages read directly via mmap()'d files.
  98. ReadPages uint64
  99. // Number of pages written directly via mmap()'d files.
  100. WritePages uint64
  101. }
  102. // A NFSEventsStats contains statistics about NFS event occurrences.
  103. type NFSEventsStats struct {
  104. // Number of times cached inode attributes are re-validated from the server.
  105. InodeRevalidate uint64
  106. // Number of times cached dentry nodes are re-validated from the server.
  107. DnodeRevalidate uint64
  108. // Number of times an inode cache is cleared.
  109. DataInvalidate uint64
  110. // Number of times cached inode attributes are invalidated.
  111. AttributeInvalidate uint64
  112. // Number of times files or directories have been open()'d.
  113. VFSOpen uint64
  114. // Number of times a directory lookup has occurred.
  115. VFSLookup uint64
  116. // Number of times permissions have been checked.
  117. VFSAccess uint64
  118. // Number of updates (and potential writes) to pages.
  119. VFSUpdatePage uint64
  120. // Number of pages read directly via mmap()'d files.
  121. VFSReadPage uint64
  122. // Number of times a group of pages have been read.
  123. VFSReadPages uint64
  124. // Number of pages written directly via mmap()'d files.
  125. VFSWritePage uint64
  126. // Number of times a group of pages have been written.
  127. VFSWritePages uint64
  128. // Number of times directory entries have been read with getdents().
  129. VFSGetdents uint64
  130. // Number of times attributes have been set on inodes.
  131. VFSSetattr uint64
  132. // Number of pending writes that have been forcefully flushed to the server.
  133. VFSFlush uint64
  134. // Number of times fsync() has been called on directories and files.
  135. VFSFsync uint64
  136. // Number of times locking has been attempted on a file.
  137. VFSLock uint64
  138. // Number of times files have been closed and released.
  139. VFSFileRelease uint64
  140. // Unknown. Possibly unused.
  141. CongestionWait uint64
  142. // Number of times files have been truncated.
  143. Truncation uint64
  144. // Number of times a file has been grown due to writes beyond its existing end.
  145. WriteExtension uint64
  146. // Number of times a file was removed while still open by another process.
  147. SillyRename uint64
  148. // Number of times the NFS server gave less data than expected while reading.
  149. ShortRead uint64
  150. // Number of times the NFS server wrote less data than expected while writing.
  151. ShortWrite uint64
  152. // Number of times the NFS server indicated EJUKEBOX; retrieving data from
  153. // offline storage.
  154. JukeboxDelay uint64
  155. // Number of NFS v4.1+ pNFS reads.
  156. PNFSRead uint64
  157. // Number of NFS v4.1+ pNFS writes.
  158. PNFSWrite uint64
  159. }
  160. // A NFSOperationStats contains statistics for a single operation.
  161. type NFSOperationStats struct {
  162. // The name of the operation.
  163. Operation string
  164. // Number of requests performed for this operation.
  165. Requests uint64
  166. // Number of times an actual RPC request has been transmitted for this operation.
  167. Transmissions uint64
  168. // Number of times a request has had a major timeout.
  169. MajorTimeouts uint64
  170. // Number of bytes sent for this operation, including RPC headers and payload.
  171. BytesSent uint64
  172. // Number of bytes received for this operation, including RPC headers and payload.
  173. BytesReceived uint64
  174. // Duration all requests spent queued for transmission before they were sent.
  175. CumulativeQueueMilliseconds uint64
  176. // Duration it took to get a reply back after the request was transmitted.
  177. CumulativeTotalResponseMilliseconds uint64
  178. // Duration from when a request was enqueued to when it was completely handled.
  179. CumulativeTotalRequestMilliseconds uint64
  180. // The average time from the point the client sends RPC requests until it receives the response.
  181. AverageRTTMilliseconds float64
  182. // The count of operations that complete with tk_status < 0. These statuses usually indicate error conditions.
  183. Errors uint64
  184. }
  185. // A NFSTransportStats contains statistics for the NFS mount RPC requests and
  186. // responses.
  187. type NFSTransportStats struct {
  188. // The transport protocol used for the NFS mount.
  189. Protocol string
  190. // The local port used for the NFS mount.
  191. Port uint64
  192. // Number of times the client has had to establish a connection from scratch
  193. // to the NFS server.
  194. Bind uint64
  195. // Number of times the client has made a TCP connection to the NFS server.
  196. Connect uint64
  197. // Duration (in jiffies, a kernel internal unit of time) the NFS mount has
  198. // spent waiting for connections to the server to be established.
  199. ConnectIdleTime uint64
  200. // Duration since the NFS mount last saw any RPC traffic.
  201. IdleTimeSeconds uint64
  202. // Number of RPC requests for this mount sent to the NFS server.
  203. Sends uint64
  204. // Number of RPC responses for this mount received from the NFS server.
  205. Receives uint64
  206. // Number of times the NFS server sent a response with a transaction ID
  207. // unknown to this client.
  208. BadTransactionIDs uint64
  209. // A running counter, incremented on each request as the current difference
  210. // ebetween sends and receives.
  211. CumulativeActiveRequests uint64
  212. // A running counter, incremented on each request by the current backlog
  213. // queue size.
  214. CumulativeBacklog uint64
  215. // Stats below only available with stat version 1.1.
  216. // Maximum number of simultaneously active RPC requests ever used.
  217. MaximumRPCSlotsUsed uint64
  218. // A running counter, incremented on each request as the current size of the
  219. // sending queue.
  220. CumulativeSendingQueue uint64
  221. // A running counter, incremented on each request as the current size of the
  222. // pending queue.
  223. CumulativePendingQueue uint64
  224. // Stats below only available with stat version 1.1.
  225. // Transport over RDMA
  226. // accessed when sending a call
  227. ReadChunkCount uint64
  228. WriteChunkCount uint64
  229. ReplyChunkCount uint64
  230. TotalRdmaRequest uint64
  231. // rarely accessed error counters
  232. PullupCopyCount uint64
  233. HardwayRegisterCount uint64
  234. FailedMarshalCount uint64
  235. BadReplyCount uint64
  236. MrsRecovered uint64
  237. MrsOrphaned uint64
  238. MrsAllocated uint64
  239. EmptySendctxQ uint64
  240. // accessed when receiving a reply
  241. TotalRdmaReply uint64
  242. FixupCopyCount uint64
  243. ReplyWaitsForSend uint64
  244. LocalInvNeeded uint64
  245. NomsgCallCount uint64
  246. BcallCount uint64
  247. }
  248. // parseMountStats parses a /proc/[pid]/mountstats file and returns a slice
  249. // of Mount structures containing detailed information about each mount.
  250. // If available, statistics for each mount are parsed as well.
  251. func parseMountStats(r io.Reader) ([]*Mount, error) {
  252. const (
  253. device = "device"
  254. statVersionPrefix = "statvers="
  255. nfs3Type = "nfs"
  256. nfs4Type = "nfs4"
  257. )
  258. var mounts []*Mount
  259. s := bufio.NewScanner(r)
  260. for s.Scan() {
  261. // Only look for device entries in this function
  262. ss := strings.Fields(string(s.Bytes()))
  263. if len(ss) == 0 || ss[0] != device {
  264. continue
  265. }
  266. m, err := parseMount(ss)
  267. if err != nil {
  268. return nil, err
  269. }
  270. // Does this mount also possess statistics information?
  271. if len(ss) > deviceEntryLen {
  272. // Only NFSv3 and v4 are supported for parsing statistics
  273. if m.Type != nfs3Type && m.Type != nfs4Type {
  274. return nil, fmt.Errorf("%w: Cannot parse MountStats for %q", ErrFileParse, m.Type)
  275. }
  276. statVersion := strings.TrimPrefix(ss[8], statVersionPrefix)
  277. stats, err := parseMountStatsNFS(s, statVersion)
  278. if err != nil {
  279. return nil, err
  280. }
  281. m.Stats = stats
  282. }
  283. mounts = append(mounts, m)
  284. }
  285. return mounts, s.Err()
  286. }
  287. // parseMount parses an entry in /proc/[pid]/mountstats in the format:
  288. //
  289. // device [device] mounted on [mount] with fstype [type]
  290. func parseMount(ss []string) (*Mount, error) {
  291. if len(ss) < deviceEntryLen {
  292. return nil, fmt.Errorf("%w: Invalid device %q", ErrFileParse, ss)
  293. }
  294. // Check for specific words appearing at specific indices to ensure
  295. // the format is consistent with what we expect
  296. format := []struct {
  297. i int
  298. s string
  299. }{
  300. {i: 0, s: "device"},
  301. {i: 2, s: "mounted"},
  302. {i: 3, s: "on"},
  303. {i: 5, s: "with"},
  304. {i: 6, s: "fstype"},
  305. }
  306. for _, f := range format {
  307. if ss[f.i] != f.s {
  308. return nil, fmt.Errorf("%w: Invalid device %q", ErrFileParse, ss)
  309. }
  310. }
  311. return &Mount{
  312. Device: ss[1],
  313. Mount: ss[4],
  314. Type: ss[7],
  315. }, nil
  316. }
  317. // parseMountStatsNFS parses a MountStatsNFS by scanning additional information
  318. // related to NFS statistics.
  319. func parseMountStatsNFS(s *bufio.Scanner, statVersion string) (*MountStatsNFS, error) {
  320. // Field indicators for parsing specific types of data
  321. const (
  322. fieldOpts = "opts:"
  323. fieldAge = "age:"
  324. fieldBytes = "bytes:"
  325. fieldEvents = "events:"
  326. fieldPerOpStats = "per-op"
  327. fieldTransport = "xprt:"
  328. )
  329. stats := &MountStatsNFS{
  330. StatVersion: statVersion,
  331. }
  332. for s.Scan() {
  333. ss := strings.Fields(string(s.Bytes()))
  334. if len(ss) == 0 {
  335. break
  336. }
  337. switch ss[0] {
  338. case fieldOpts:
  339. if len(ss) < 2 {
  340. return nil, fmt.Errorf("%w: Incomplete information for NFS stats: %v", ErrFileParse, ss)
  341. }
  342. if stats.Opts == nil {
  343. stats.Opts = map[string]string{}
  344. }
  345. for _, opt := range strings.Split(ss[1], ",") {
  346. split := strings.Split(opt, "=")
  347. if len(split) == 2 {
  348. stats.Opts[split[0]] = split[1]
  349. } else {
  350. stats.Opts[opt] = ""
  351. }
  352. }
  353. case fieldAge:
  354. if len(ss) < 2 {
  355. return nil, fmt.Errorf("%w: Incomplete information for NFS stats: %v", ErrFileParse, ss)
  356. }
  357. // Age integer is in seconds
  358. d, err := time.ParseDuration(ss[1] + "s")
  359. if err != nil {
  360. return nil, err
  361. }
  362. stats.Age = d
  363. case fieldBytes:
  364. if len(ss) < 2 {
  365. return nil, fmt.Errorf("%w: Incomplete information for NFS stats: %v", ErrFileParse, ss)
  366. }
  367. bstats, err := parseNFSBytesStats(ss[1:])
  368. if err != nil {
  369. return nil, err
  370. }
  371. stats.Bytes = *bstats
  372. case fieldEvents:
  373. if len(ss) < 2 {
  374. return nil, fmt.Errorf("%w: Incomplete information for NFS events: %v", ErrFileParse, ss)
  375. }
  376. estats, err := parseNFSEventsStats(ss[1:])
  377. if err != nil {
  378. return nil, err
  379. }
  380. stats.Events = *estats
  381. case fieldTransport:
  382. if len(ss) < 3 {
  383. return nil, fmt.Errorf("%w: Incomplete information for NFS transport stats: %v", ErrFileParse, ss)
  384. }
  385. tstats, err := parseNFSTransportStats(ss[1:], statVersion)
  386. if err != nil {
  387. return nil, err
  388. }
  389. stats.Transport = *tstats
  390. }
  391. // When encountering "per-operation statistics", we must break this
  392. // loop and parse them separately to ensure we can terminate parsing
  393. // before reaching another device entry; hence why this 'if' statement
  394. // is not just another switch case
  395. if ss[0] == fieldPerOpStats {
  396. break
  397. }
  398. }
  399. if err := s.Err(); err != nil {
  400. return nil, err
  401. }
  402. // NFS per-operation stats appear last before the next device entry
  403. perOpStats, err := parseNFSOperationStats(s)
  404. if err != nil {
  405. return nil, err
  406. }
  407. stats.Operations = perOpStats
  408. return stats, nil
  409. }
  410. // parseNFSBytesStats parses a NFSBytesStats line using an input set of
  411. // integer fields.
  412. func parseNFSBytesStats(ss []string) (*NFSBytesStats, error) {
  413. if len(ss) != fieldBytesLen {
  414. return nil, fmt.Errorf("%w: Invalid NFS bytes stats: %v", ErrFileParse, ss)
  415. }
  416. ns := make([]uint64, 0, fieldBytesLen)
  417. for _, s := range ss {
  418. n, err := strconv.ParseUint(s, 10, 64)
  419. if err != nil {
  420. return nil, err
  421. }
  422. ns = append(ns, n)
  423. }
  424. return &NFSBytesStats{
  425. Read: ns[0],
  426. Write: ns[1],
  427. DirectRead: ns[2],
  428. DirectWrite: ns[3],
  429. ReadTotal: ns[4],
  430. WriteTotal: ns[5],
  431. ReadPages: ns[6],
  432. WritePages: ns[7],
  433. }, nil
  434. }
  435. // parseNFSEventsStats parses a NFSEventsStats line using an input set of
  436. // integer fields.
  437. func parseNFSEventsStats(ss []string) (*NFSEventsStats, error) {
  438. if len(ss) != fieldEventsLen {
  439. return nil, fmt.Errorf("%w: invalid NFS events stats: %v", ErrFileParse, ss)
  440. }
  441. ns := make([]uint64, 0, fieldEventsLen)
  442. for _, s := range ss {
  443. n, err := strconv.ParseUint(s, 10, 64)
  444. if err != nil {
  445. return nil, err
  446. }
  447. ns = append(ns, n)
  448. }
  449. return &NFSEventsStats{
  450. InodeRevalidate: ns[0],
  451. DnodeRevalidate: ns[1],
  452. DataInvalidate: ns[2],
  453. AttributeInvalidate: ns[3],
  454. VFSOpen: ns[4],
  455. VFSLookup: ns[5],
  456. VFSAccess: ns[6],
  457. VFSUpdatePage: ns[7],
  458. VFSReadPage: ns[8],
  459. VFSReadPages: ns[9],
  460. VFSWritePage: ns[10],
  461. VFSWritePages: ns[11],
  462. VFSGetdents: ns[12],
  463. VFSSetattr: ns[13],
  464. VFSFlush: ns[14],
  465. VFSFsync: ns[15],
  466. VFSLock: ns[16],
  467. VFSFileRelease: ns[17],
  468. CongestionWait: ns[18],
  469. Truncation: ns[19],
  470. WriteExtension: ns[20],
  471. SillyRename: ns[21],
  472. ShortRead: ns[22],
  473. ShortWrite: ns[23],
  474. JukeboxDelay: ns[24],
  475. PNFSRead: ns[25],
  476. PNFSWrite: ns[26],
  477. }, nil
  478. }
  479. // parseNFSOperationStats parses a slice of NFSOperationStats by scanning
  480. // additional information about per-operation statistics until an empty
  481. // line is reached.
  482. func parseNFSOperationStats(s *bufio.Scanner) ([]NFSOperationStats, error) {
  483. const (
  484. // Minimum number of expected fields in each per-operation statistics set
  485. minFields = 9
  486. )
  487. var ops []NFSOperationStats
  488. for s.Scan() {
  489. ss := strings.Fields(string(s.Bytes()))
  490. if len(ss) == 0 {
  491. // Must break when reading a blank line after per-operation stats to
  492. // enable top-level function to parse the next device entry
  493. break
  494. }
  495. if len(ss) < minFields {
  496. return nil, fmt.Errorf("%w: invalid NFS per-operations stats: %v", ErrFileParse, ss)
  497. }
  498. // Skip string operation name for integers
  499. ns := make([]uint64, 0, minFields-1)
  500. for _, st := range ss[1:] {
  501. n, err := strconv.ParseUint(st, 10, 64)
  502. if err != nil {
  503. return nil, err
  504. }
  505. ns = append(ns, n)
  506. }
  507. opStats := NFSOperationStats{
  508. Operation: strings.TrimSuffix(ss[0], ":"),
  509. Requests: ns[0],
  510. Transmissions: ns[1],
  511. MajorTimeouts: ns[2],
  512. BytesSent: ns[3],
  513. BytesReceived: ns[4],
  514. CumulativeQueueMilliseconds: ns[5],
  515. CumulativeTotalResponseMilliseconds: ns[6],
  516. CumulativeTotalRequestMilliseconds: ns[7],
  517. }
  518. if ns[0] != 0 {
  519. opStats.AverageRTTMilliseconds = float64(ns[6]) / float64(ns[0])
  520. }
  521. if len(ns) > 8 {
  522. opStats.Errors = ns[8]
  523. }
  524. ops = append(ops, opStats)
  525. }
  526. return ops, s.Err()
  527. }
  528. // parseNFSTransportStats parses a NFSTransportStats line using an input set of
  529. // integer fields matched to a specific stats version.
  530. func parseNFSTransportStats(ss []string, statVersion string) (*NFSTransportStats, error) {
  531. // Extract the protocol field. It is the only string value in the line
  532. protocol := ss[0]
  533. ss = ss[1:]
  534. switch statVersion {
  535. case statVersion10:
  536. var expectedLength int
  537. if protocol == "tcp" {
  538. expectedLength = fieldTransport10TCPLen
  539. } else if protocol == "udp" {
  540. expectedLength = fieldTransport10UDPLen
  541. } else {
  542. return nil, fmt.Errorf("%w: Invalid NFS protocol \"%s\" in stats 1.0 statement: %v", ErrFileParse, protocol, ss)
  543. }
  544. if len(ss) != expectedLength {
  545. return nil, fmt.Errorf("%w: Invalid NFS transport stats 1.0 statement: %v", ErrFileParse, ss)
  546. }
  547. case statVersion11:
  548. var expectedLength int
  549. if protocol == "tcp" {
  550. expectedLength = fieldTransport11TCPLen
  551. } else if protocol == "udp" {
  552. expectedLength = fieldTransport11UDPLen
  553. } else if protocol == "rdma" {
  554. expectedLength = fieldTransport11RDMAMinLen
  555. } else {
  556. return nil, fmt.Errorf("%w: invalid NFS protocol \"%s\" in stats 1.1 statement: %v", ErrFileParse, protocol, ss)
  557. }
  558. if (len(ss) != expectedLength && (protocol == "tcp" || protocol == "udp")) ||
  559. (protocol == "rdma" && len(ss) < expectedLength) {
  560. return nil, fmt.Errorf("%w: invalid NFS transport stats 1.1 statement: %v, protocol: %v", ErrFileParse, ss, protocol)
  561. }
  562. default:
  563. return nil, fmt.Errorf("%s: Unrecognized NFS transport stats version: %q, protocol: %v", ErrFileParse, statVersion, protocol)
  564. }
  565. // Allocate enough for v1.1 stats since zero value for v1.1 stats will be okay
  566. // in a v1.0 response. Since the stat length is bigger for TCP stats, we use
  567. // the TCP length here.
  568. //
  569. // Note: slice length must be set to length of v1.1 stats to avoid a panic when
  570. // only v1.0 stats are present.
  571. // See: https://github.com/prometheus/node_exporter/issues/571.
  572. //
  573. // Note: NFS Over RDMA slice length is fieldTransport11RDMAMaxLen
  574. ns := make([]uint64, fieldTransport11RDMAMaxLen+3)
  575. for i, s := range ss {
  576. n, err := strconv.ParseUint(s, 10, 64)
  577. if err != nil {
  578. return nil, err
  579. }
  580. ns[i] = n
  581. }
  582. // The fields differ depending on the transport protocol (TCP or UDP)
  583. // From https://utcc.utoronto.ca/%7Ecks/space/blog/linux/NFSMountstatsXprt
  584. //
  585. // For the udp RPC transport there is no connection count, connect idle time,
  586. // or idle time (fields #3, #4, and #5); all other fields are the same. So
  587. // we set them to 0 here.
  588. if protocol == "udp" {
  589. ns = append(ns[:2], append(make([]uint64, 3), ns[2:]...)...)
  590. } else if protocol == "tcp" {
  591. ns = append(ns[:fieldTransport11TCPLen], make([]uint64, fieldTransport11RDMAMaxLen-fieldTransport11TCPLen+3)...)
  592. } else if protocol == "rdma" {
  593. ns = append(ns[:fieldTransport10TCPLen], append(make([]uint64, 3), ns[fieldTransport10TCPLen:]...)...)
  594. }
  595. return &NFSTransportStats{
  596. // NFS xprt over tcp or udp
  597. Protocol: protocol,
  598. Port: ns[0],
  599. Bind: ns[1],
  600. Connect: ns[2],
  601. ConnectIdleTime: ns[3],
  602. IdleTimeSeconds: ns[4],
  603. Sends: ns[5],
  604. Receives: ns[6],
  605. BadTransactionIDs: ns[7],
  606. CumulativeActiveRequests: ns[8],
  607. CumulativeBacklog: ns[9],
  608. // NFS xprt over tcp or udp
  609. // And statVersion 1.1
  610. MaximumRPCSlotsUsed: ns[10],
  611. CumulativeSendingQueue: ns[11],
  612. CumulativePendingQueue: ns[12],
  613. // NFS xprt over rdma
  614. // And stat Version 1.1
  615. ReadChunkCount: ns[13],
  616. WriteChunkCount: ns[14],
  617. ReplyChunkCount: ns[15],
  618. TotalRdmaRequest: ns[16],
  619. PullupCopyCount: ns[17],
  620. HardwayRegisterCount: ns[18],
  621. FailedMarshalCount: ns[19],
  622. BadReplyCount: ns[20],
  623. MrsRecovered: ns[21],
  624. MrsOrphaned: ns[22],
  625. MrsAllocated: ns[23],
  626. EmptySendctxQ: ns[24],
  627. TotalRdmaReply: ns[25],
  628. FixupCopyCount: ns[26],
  629. ReplyWaitsForSend: ns[27],
  630. LocalInvNeeded: ns[28],
  631. NomsgCallCount: ns[29],
  632. BcallCount: ns[30],
  633. }, nil
  634. }