Skip to content

Instantly share code, notes, and snippets.

@jizhilong
jizhilong / timerfd.py
Last active February 20, 2024 05:32
timerfd_create/timerfd_settime/timerfd_gettime implementations based on ctypes and libc
"""
timerfd_xx functions and demos for python versions prior to 3.13, implemented with ctypes and libc.
"""
try:
from os import timerfd_create, timerfd_settime, timerfd_gettime, timerfd_settime_ns, timerfd_gettime_ns
from os import TFD_NONBLOCK, TFD_CLOEXEC, TFD_TIMER_ABSTIME, TFD_TIMER_CANCEL_ON_SET
except ImportError:
import os
import time
from typing import Optional
@jizhilong
jizhilong / gitlab-ci-job-rules-cheatsheet.md
Created December 22, 2021 07:52
gitlab ci job rules cheat sheet

only if

only master branch

rules:
  - if: '$CI_COMMIT_BRANCH == "master"'
    when: always
  - when: never

only tags

@jizhilong
jizhilong / cswsnoop_jvm.py
Created May 25, 2020 11:45
count and sort jvm threadpool's context switch rate
#!/usr/bin/env python
"""
Author: Ji.Zhilong <zhilongji@gmail.com>
Description: count and sort jvm threadpool's context switch rate
Usage: python cswsnoop_jvm.py
"""
import sys
import time
import os
@jizhilong
jizhilong / cswsnoop
Last active October 19, 2019 07:47
snoop commands with context switch freqency more than 1000/s
#!/usr/bin/env python
'''
count occurence rate of lines with certain keyword with ewma algorithm.
'''
import math
import time
import sys
@jizhilong
jizhilong / Parsers.java
Created October 13, 2019 05:51
a simple parsec like parer combinator toll in java 8
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@jizhilong
jizhilong / ewma.py
Created September 20, 2019 04:04
cli tool for counting occurence rate of lines with certain keyword with ewma algorithm.
#!/usr/bin/env python
'''
count occurence rate of lines with certain keyword with ewma algorithm.
'''
import math
import time
import sys
@jizhilong
jizhilong / gen_git_crypt_key.py
Created May 11, 2017 06:45
a python script for generating git crypt key file from password.
#!/usr/bin/env python
import os
import struct
import hashlib
import hmac
import sys
import getpass
HMAC_KEY_FOR_AES = '\xa7\x8fd\xaa\xa9\x9cwWnjf\xf6bN\xb0\xb7<a\xdb\xd8\xbf\xc7\x99\xaf\xc1)\x96\xf8\xe4i`\xc3l\x94l7h'
import os
import glob
import shutil
def rotate_files(filename, limit):
need_rotate = os.path.exists(filename)
if not need_rotate:
return
files = glob.glob('%s.*' % filename)
files.sort(key=lambda f: int(f.rsplit('.', 1)[1]))
@jizhilong
jizhilong / reverse_words
Created October 27, 2013 12:56
my solution to the classic interview question: "reverse the words in a string , and keep the characters' order within a word unchanged."
/*
* =====================================================================================
*
* Filename: reverse_words.cpp
*
* Description: a program to reverse the words in a sentence.
*
* Version: 1.0
* Created: 10/27/2013 08:21:49 PM
* Revision: none
@jizhilong
jizhilong / kmp.cpp
Created October 4, 2013 10:36
Knuth-Morris-Pratt Algorithm
/*
* =====================================================================================
*
* Filename: kmp.cpp
*
* Description: Knuth-Morris-Pratt Algorithm
*
* Version: 1.0
* Created: 10/04/2013 06:15:30 PM
* Revision: none