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

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