Skip to content

Instantly share code, notes, and snippets.

@gekigek99
gekigek99 / golang package import graph.py
Last active March 3, 2023 10:24
generate package import graph of golang module with python graphviz
# generate package import graph of golang module with python graphviz.
# script should be run in the directory where main.go is located.
import os
import graphviz
import subprocess
import json
import time
describe_tag = subprocess.check_output(["git", "describe", "--tags"]).decode("utf-8").strip()
@gekigek99
gekigek99 / cpu usage percent.go
Last active March 3, 2023 18:07
calculate the average cpu usage percent of the last second of a given pid
// This script calculates the average cpu usage percent of the last second (of a given pid, in this case the script itself).
// (*process.Process).CPUPercent() from "github.com/shirou/gopsutil/process" returns the average cpu usage percent since the process was started.
// If you call cpuPercent(p) every 10 seconds then the average cpu usage percent of the last 10 seconds is returned.
//
// cpuPercent first call returns the average cpu percent usage since the start of the process.
//
// Remember that if the process you are analyzing changes pid you have to update `pTracker` with the new pid (+new info) and remove the old pid.
// (not the case for this example though)
package main
@gekigek99
gekigek99 / suspend process tree by pid (windows).go
Last active December 14, 2021 12:45
suspend process tree by pid (windows)
// This is a script to suspend the child processes in a process tree
// Tn this example a minecraft process is used as example
// (windows specific)
package main
import (
"bufio"
"fmt"
"os/exec"
@gekigek99
gekigek99 / get process tree pids.go
Last active April 21, 2023 13:09
Get the child processes pids of a process (retrieve the complete process tree)
// This is a script to retrieve the child processes pids of a process (retrieve the complete process tree)
// Tn this example the process identified by pid 7400 is used
// (please to run the test chose a process pid that has at least 1 child process)
// (tested on windows)
package main
import (
"fmt"
"syscall"
@gekigek99
gekigek99 / withLock.go
Created February 10, 2021 09:06
function to execute entire functions with a locked mutex
// this script can be use to execute entire functions with a locked mutex.
// It is especially useful when a if statement needs to read from a mutex sensible variable.
// withLock accepts either a "func()" or a "func() interface{}"
// in the first case, it executes the function returining <nil>
// in the second case, it returns the value of the function as an interface{}
package main
import (