pkg.re/essentialkaos/ek@v12.36.0+incompatible/system/process/examples_test.go (about)

     1  // +build linux
     2  
     3  package process
     4  
     5  // ////////////////////////////////////////////////////////////////////////////////// //
     6  //                                                                                    //
     7  //                         Copyright (c) 2021 ESSENTIAL KAOS                          //
     8  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     9  //                                                                                    //
    10  // ////////////////////////////////////////////////////////////////////////////////// //
    11  
    12  import (
    13  	"fmt"
    14  	"os"
    15  	"time"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  func ExampleGetTree() {
    21  	process, err := GetTree()
    22  
    23  	if err != nil {
    24  		return
    25  	}
    26  
    27  	// process is a top process in the tree
    28  	fmt.Printf("%v\n", process.PID)
    29  }
    30  
    31  func ExampleGetList() {
    32  	processes, err := GetList()
    33  
    34  	if err != nil {
    35  		return
    36  	}
    37  
    38  	// processes is slice with info about all active processes
    39  	fmt.Printf("%v\n", processes)
    40  }
    41  
    42  func ExampleCalculateCPUUsage() {
    43  	pid := 1345
    44  	duration := time.Second * 15
    45  
    46  	sample1, _ := GetSample(pid)
    47  	time.Sleep(duration)
    48  	sample2, _ := GetSample(pid)
    49  
    50  	fmt.Printf("CPU Usage: %g%%\n", CalculateCPUUsage(sample1, sample2, duration))
    51  }
    52  
    53  func ExampleGetMemInfo() {
    54  	info, err := GetMemInfo(1000)
    55  
    56  	if err != nil {
    57  		return
    58  	}
    59  
    60  	fmt.Printf("%v\n", info)
    61  }
    62  
    63  func ExampleGetMountInfo() {
    64  	info, err := GetMountInfo(1000)
    65  
    66  	if err != nil {
    67  		return
    68  	}
    69  
    70  	fmt.Printf("%v\n", info)
    71  }
    72  
    73  func ExampleGetCPUPriority() {
    74  	pid := os.Getpid()
    75  	pr, ni, err := GetCPUPriority(pid)
    76  
    77  	if err != nil {
    78  		return
    79  	}
    80  
    81  	fmt.Printf("PR: %d | NI: %d\n", pr, ni)
    82  }
    83  
    84  func ExampleSetCPUPriority() {
    85  	pid := os.Getpid()
    86  	err := SetCPUPriority(pid, -20)
    87  
    88  	if err != nil {
    89  		return
    90  	}
    91  
    92  	pr, ni, err := GetCPUPriority(pid)
    93  
    94  	if err != nil {
    95  		return
    96  	}
    97  
    98  	fmt.Printf("PR: %d | NI: %d\n", pr, ni)
    99  }
   100  
   101  func ExampleGetIOPriority() {
   102  	pid := os.Getpid()
   103  	class, classdata, err := GetIOPriority(pid)
   104  
   105  	if err != nil {
   106  		return
   107  	}
   108  
   109  	fmt.Printf("Class: %d | Classdata: %d\n", class, classdata)
   110  }
   111  
   112  func ExampleSetIOPriority() {
   113  	pid := os.Getpid()
   114  	err := SetIOPriority(pid, PRIO_CLASS_REAL_TIME, 5)
   115  
   116  	if err != nil {
   117  		return
   118  	}
   119  
   120  	class, classdata, err := GetIOPriority(pid)
   121  
   122  	if err != nil {
   123  		return
   124  	}
   125  
   126  	fmt.Printf("Class: %d | Classdata: %d\n", class, classdata)
   127  }