systemd's logo

Running systemd Units Only If Certain CPU Features Are Available

systemd鈥檚 logo (C) https://brand.systemd.io/ Do you have some kind of application that should only be executed when the user鈥檚 CPU has a certain feature? Or maybe your application has horrible performance unless some certain instructions are available? As you might know already, the Linux kernel exposes this information via /proc/cpuinfo: processor : 15 vendor_id : AuthenticAMD cpu family : 23 model : 96 model name : AMD Ryzen 7 PRO 4750U with Radeon Graphics ... flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd _apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce to poext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xge tbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip rdpid overflow_recov succor smca Where does this data come from? One source of information is the cpuid instruction. On the x86 architecture systemd starting from v248 is now able to start or stop an unit based on the features exposed by the CPUID instruction. Enter ConditionCPUFeature. On CPUs that do not have the CPUID instruction i.e. ARM CPUs, it is assumed that the CPU implements no features and thus any potential CPUFeature conditions will fail. ...

February 19, 2021 路 3 min 路 giedrius

scanf(3) is a newbie trap, use fgets(3) with sscanf(3) instead

If you have not known before, scanf(3) and fgets(3) are both functions intended for reading something from standard input and doing something with the result - in the former case it is interpreted and the results might possibly be stored in specified arguments and in the latter case the result is simply put into a buffer. The first one is commonly recommended to beginner C programmers for reading something from the user and parsing it. However, it should be avoided mainly because it is very error-prone and it is difficult to understand how it actually works for new people. Instead of scanf(3), sscanf(3) should be used in combination with fgets(3). That way, you can easily guess in what state your standard input stream is after using those functions and you get other benefits. Let me give some examples and explain more. ...

June 15, 2018 路 4 min 路 giedrius

Why os.move() Sometimes Does Not Work And Why shutil.move() Is The Savior?

Recently I ran into an issue where this, for example, code fails: import os os.rename('/foo/a.txt', '/bar/b.txt') Traceback (most recent call last): File "", line 1, in OSError: [Errno 18] Invalid cross-device link The聽documentation聽for the聽os module says that sometimes it might fail when the source and the destination are on different file-systems: os.rename( src,聽dst,聽*,聽src_dir_fd=None,聽dst_dir_fd=None) Rename the file or directory聽src聽to聽dst. If聽dst聽is a directory,聽OSError聽will be raised. On Unix, if聽dst聽exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if聽src聽and聽dst聽are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if聽dst聽already exists,聽OSError聽will be raised even if it is a file. ...

January 28, 2018 路 5 min 路 giedrius

Making Unwinding Functions in C Simple: Do Not be Afraid of Using Gotos

Intro Today I wanted to talk about unwinding and releasing resources in C functions. Let鈥檚 begin by stating that there are three main techniques for handling errors in the C programming language. Sometimes more than one technique may be used. Here is a list of them: You must test the value functions return. Abnormal value indicates that some kind of error has happened and a normal value indicates that it was successful; There is an external variable whose value you must check. For example, the POSIX variant of this is to have an variable called errno that changes to 0 when nothing bad happened and it has some kind of other value when an error occurs; You pass a pointer to a function. The function changes the value of the variable it points to or even calls it with certain arguments if it is a function pointer depending on the result. I have not mentioned one method but some people use atexit(3) to register functions that will be called at the end of a program which will release resources. However, this is unusual so I have not included it in the list.This is very much related to our topic because when an error occurs, you will have to handle it. That process includes releasing the resources which were acquired before in the function. Especially if you are deep down in your function and then an error occurred, the choice that you make in how to release the resources will matter a lot so it is important to make the correct decision. In C++ you have the destructors and so on but how are you going to do that in C? Are you going to sprinkle all of your error paths with: ...

October 22, 2017 路 7 min 路 giedrius