Signed-off-by: allencloud <allen.sun@daocloud.io>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+package fileutils |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "os" |
|
| 4 |
+ "os/exec" |
|
| 5 |
+ "strconv" |
|
| 6 |
+ "strings" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+// GetTotalUsedFds returns the number of used File Descriptors by |
|
| 10 |
+// executing `lsof -p PID` |
|
| 11 |
+func GetTotalUsedFds() int {
|
|
| 12 |
+ pid := os.Getpid() |
|
| 13 |
+ |
|
| 14 |
+ cmd := exec.Command("lsof", "-p", strconv.Itoa(pid))
|
|
| 15 |
+ |
|
| 16 |
+ output, err := cmd.CombinedOutput() |
|
| 17 |
+ if err != nil {
|
|
| 18 |
+ return -1 |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 21 |
+ outputStr := strings.TrimSpace(string(output)) |
|
| 22 |
+ |
|
| 23 |
+ fds := strings.Split(outputStr, "\n") |
|
| 24 |
+ |
|
| 25 |
+ return len(fds) - 1 |
|
| 26 |
+} |