Wednesday, July 26, 2023

Starting a new adventure...

I started this blog back in 2011 as a sandbox for my occasional HOWTOs. I have grown since then as a person and an engineer. I decided it is time to move from this platform and start a new adventure.

rdiachenko.com is a new place where I'm going to post and keep new writings.

I'll keep the current blog as it is. There is still value in few posts. However, nothing will be published here anymore.

Saturday, February 8, 2020

Retrieve data from a Map reverse-ordered by value and then ordered by key


Problem

Given a Map of strings to their frequencies. Find top N-most frequent strings. If there is a tie get lexicographically smallest names first.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
List<String> findTopNNames(Map<String, Integer> data, int n) {
    Comparator<Map.Entry<String, Integer>> cmpByValueDesc =
        Map.Entry.comparingByValue(Comparator.reverseOrder());

    Comparator<Map.Entry<String, Integer>> cmpByKey =
        Map.Entry.comparingByKey();

    Comparator<Map.Entry<String, Integer>> cmp =
        cmpByValueDesc.thenComparing(cmpByKey);

    return data.entrySet().stream()
        .sorted(cmp)
        .map(Map.Entry::getKey)
        .limit(n)
        .collect(Collectors.toList());
}

Tuesday, February 4, 2020

[WORKAROUND] Could not load GLX/OpenGL functions /usr/lib64/VirtualGL/libvglfaker.so: undefined symbol: glXGetProcAddressARB

I've encountered the following problem while starting IntelliJ IDEA via optirun:

$ optirun -c yuv ./idea-IC-193.5662.53/bin/idea.sh
...
[VGL] ERROR: Could not load GLX/OpenGL functions
[VGL] /usr/lib64/VirtualGL/libvglfaker.so: undefined symbol: glXGetProcAddressARB

Quick search gave me this link - https://github.com/VirtualGL/virtualgl/issues/102

Checked VirtualGL version:

$ rpm -qi VirtualGL
VirtualGL-2.5.2-4.fc31.x86_64

Added original VirtualGL yum repo according to https://virtualgl.org/Downloads/YUM

$ cat /etc/yum.repos.d/VirtualGL.repo 
[VirtualGL]
name=VirtualGL official RPMs
baseurl=https://sourceforge.net/projects/virtualgl/files
gpgcheck=1
gpgkey=https://sourceforge.net/projects/virtualgl/files/VGL-GPG-KEY
enabled=1
exclude=VirtualGL-*.*.9[0-9]-*

Thursday, January 30, 2020

[SOLVED] Cannot access secondary GPU - error: [XORG] (EE) Failed to load module "nvidia" (module does not exist, 0)

$ optirun -b none nvidia-settings -c :8
[   70.861063] [ERROR]Cannot access secondary GPU - error: [XORG] (EE) \
Failed to load module "nvidia" (module does not exist, 0)

[   70.861138] [ERROR]Aborting because fallback start is disabled.

The above is the problem I've encountered recently during NVIDIA proprietary driver installation on Fedora 31. Here're some details about a kernel and graphics card:

$ uname -r
5.4.13-201.fc31.x86_64

$ lspci -vnn | grep '\''[030[02]\]'
00:02.0 VGA compatible controller [0300]: Intel Corporation 3rd Gen Core \
processor Graphics Controller [8086:0166] (rev 09) (prog-if 00 [VGA controller])

01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GK107M \
[GeForce GT 650M] [10de:0fd1] (rev ff) (prog-if ff)


I followed these instructions - https://docs.fedoraproject.org/en-US/quick-docs/bumblebee/index.html#_installation_nvidia_proprietary_driver. But after system restart "nvidia" module failed to load.

Here's a good troubleshooting page - https://github.com/Bumblebee-Project/Bumblebee/wiki/Troubleshooting. But there was nothing related to the mentioned error.

After reviewing bumblebee config I found out that "nvidia" module was actually missed:

$ grep "XorgModulePath" /etc/bumblebee/bumblebee.conf 
XorgModulePath=/usr/lib64/nvidia/xorg,/usr/lib64/xorg/modules

$ find /usr/lib64/xorg/modules -iname *nvidia*
$

Installing nvidia-driver and restarting the system solved the problem:

$ sudo dnf install nvidia-driver

$ find /usr/lib64/xorg/modules -iname *nvidia*
/usr/lib64/xorg/modules/drivers/nvidia_drv.so
/usr/lib64/xorg/modules/extensions/libglxserver_nvidia.so

Friday, October 4, 2019

How to merge multiple PDFs into a single PDF

Having multiple PDF files you can merge them into a single PDF file using pdfunite command.

Example:

pdfunite 1.pdf 2.pdf 3.pdf result.pdf

The command is included in poppler-utils package and is available in Fedora out of the box. If missed, can be installed as follows:

sudo dnf install poppler-utils

Friday, February 8, 2019

Book review: Clean Architecture

This was a startup. We worked 70 to 80 hours per week. We had the vision. We had the motivation. We had the will. We had the energy. We had the expertise. We had equity. We had dreams of being millionaires. We were full of shit.
Uncle Bob

This is an amazing book! It leads you through all the aspects of software structure and design. The book starts with a definition of an architecture and basic blocks it is built on. It jumps into design principles and describes them in a very detailed way using valuable examples all along the pages. The book promotes a plugin architecture; explains different boundaries you have to consider before starting the actual development; advises you to protect your business rules from the details like frameworks, databases, web; talks about the dependency rule, and much more. The chapter about clean embedded architecture will definitely give embedded system engineers food for thought. The last autobiographical chapter is especially awesome, Uncle Bob shares with his story which started in the late 1960s ...

I was impressed by the simplicity with which the whole book is written. The author's experience amazes. I thought I knew SOLID principles but after reading the corresponding chapter I was surprised how wrong has been I.

You must definitely read this book if you are either experienced developer, or just curious about software design, or you are at the start of your career as a software engineer.

Time spent on the book: ~15 hours

Tuesday, February 5, 2019

Book review: Kafka The Definitive Guide

The book describes different aspects of Kafka. It shows how to install Kafka and the stuff you need to start it. Chapters 3-5 give you the general picture of how Kafka works, parts it consists of, overview of producers and consumers and how they can be used in Java code. The chapter 5 (Kafka internals) is really nice and written in a clear and understandable way. It explains basic blocks and approaches Kafka is built on. I should also highlight the last chapter which is about stream processing. It presents common design patterns and explains few examples where Kafka streams are really good. Everything else is about Kafka configuration and monitoring. You will definitely feel overwhelmed while going through that information.

I should admit that I've never worked with Kafka and expected to get needed knowledge to start using it after reading the book. But only few chapters, mentioned above, gave me the needed understanding and were valuable for me at this stage. Kafka is not that simple as it may look. There are a lot of configuration properties and trade-offs you should consider when using it. One of the conclusions I ended up after reading this book is that it is incredibly easy to shoot yourself in the foot while configuring Kafka.

I would recommend chapters 3-5, 11 if you are new to Kafka world. Other chapters can be used as references during the actual work with Kafka. The book provides a lot of problems you might encounter and explains how to deal with them. So, you may review the trade-offs behind those problems in detail when you really face with them.

Time spent on the book: ~20 hours

Monday, January 21, 2019

Book review: Java Persistence With Hibernate (Second Edition)

A really nice book about Hibernate and its internals. It starts with the ORM concept, problems this concept tries to solve and main trade-offs. Authors introduce a simple auction application and explain how to design domain models and their relationships using Hibernate and this application all around the book. There are many approaches to solve the same problem and this book shows them and explains the pros and cons behind the solutions you can apply. A few chapters go even beyond the Hibernate itself by giving overview of the fundamental knowledge of database transactions, ACID attributes, locks, isolation levels and caching.

This is my first book in Hibernate and I had had rough knowledge before I read it. It helped me to structure my knowledge, helped me to look under the hood and see how things actually work. I finally understood those occasional exceptions we got at my previous project and parts which were designed in a wrong way. However, there is one thing I would like to see better explained - a second-level cache. It would be nice to see it earlier in the book and get more examples and details about its applications and trade-offs.

I would definitely recommend this book to all levels of engineers. It is also a good source to refresh your Hibernate knowledge or to come back later if you need some clarifications.

Time spent on the book: ~39 hours

Saturday, August 12, 2017

Triangle rasterization under the hood (the old-school approach)

I didn't find a single source which would gave me a complete and clear picture of how to rasterize a triangle in a simplest way.

So, here's my attempt to put everything in order.

Consider, that we know how to draw a line and nothing more. Here's the corresponding method declaration for it:

void drawLine(int x0, int y0, int x1, int y1, Color color);

We need to implement a method which will draw a triangle and fill it with a specified color. Here's the corresponding method declaration:

void drawTriangle(Vec2 v0, Vec2 v1, Vec2 v2, Color color);

Here's the visual representation of what this method should do:

Initial vertices
Rasterized triangle


drawTriangle(v0, v1, v2, Color.BLACK)
============================>








Saturday, March 4, 2017

JLV 1.4.0 released


Eclipse Marketplace: http://marketplace.eclipse.org/content/jlv

Project's page: https://github.com/rdiachenko/jlv#information

Release notes:
  • made plugin work on Eclipse 4.6
  • added support for logback and log4j2
  • redesigned detailed log view

Thursday, February 9, 2017

How to connect Sony LT25i to Android Studio under Fedora 24

0. Turn on USB debugging mode via Settings > Developer options

1. Connect smartphone to PC via USB and get vendor's id

$ lsusb
...
Bus 003 Device 013: ID 0fce:6186 Sony Ericsson Mobile Communications AB
...

Note: here vendor's id is: 0fce

2. Create udev rules file to allow Fedora to detect device

$ cat /etc/udev/rules.d/51-android.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="0fce", MODE="0666", GROUP="rdiachenko"

Note: here you need to change GROUP onto your own

Wednesday, October 26, 2016

Minesweeper for Linux (just for fun)

Much time has been passed since I last played minesweeper on my Windows machine in the campus... A lot has been changed since that time. Now I use Linux and I'm not in the campus any more :) I just thought: why not to implement this cool game on my own...

Here's what I have now (sources and README):


In this post I would like to share with some interesting moments I faced with during implementation.

Saturday, June 11, 2016

How many numbers in the given interval with a bit set

Problem: given two numbers a and b find how many numbers with a bit x set are there in the interval [a,b].

Example:

Input: 5 8 2

Output: 3

Explanation:

5 = 0101
6 = 0110
7 = 0111
8 = 1000

The second bit (starting from the least significant bit with index 0) is set only in numbers 5, 6 and 7, so there are 3 numbers in the interval [5, 8] with the second bit set.

Solution:

Thursday, March 3, 2016

Remove all merged local branches in Git except master


$ git branch
  a
  b
  c
* master

Here, branch 'c' has not merged with master yet. Let's remove all merged branches:

$ git branch --merged master | grep -v master | xargs git branch -d
Deleted branch a (was ebb9040).
Deleted branch b (was ebb9040).

$ git branch
  c
* master

Sunday, January 31, 2016

A fast way to find out whether two elements in matrix share the same diagonal

Let's have the following matrix:

     1 2 3 4 5
     -----------
1 | 0 0 0 0 0
2 | 0 0 1 0 0
3 | 0 0 0 0 0
4 | 1 0 0 0 0
5 | 0 0 0 0 0

with two points: (x1, y1) = (2, 3) and (x2, y2) = (4, 1).

The fastest way to find out whether these two points share the same diagonal is the result of this expression:

bool result = (x1 + y1 == x2 + y2) or (x1 - y1 == x2 - y2)

Now let's complicate the task. Given matrix n x n with m points, find the minimum number of diagonals which contain at least two points. The first line of the input contains: n m. The next m lines contain points with coordinates (xi, yi).

Saturday, January 16, 2016

dmalloc: compiling and installing a dynamic (shared) library

See the previous post: dmalloc: compiling and installing a static library

"dmalloc.h:484:7: error: expected identifier or..." problem is already solved in that post.

Now let's compile and install a shared library

dmalloc-5.5.2]$ ./configure --enable-shlib

dmalloc-5.5.2]$ make shlib
...
gcc -g -O2 -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1 -I. -I. -c dmalloc_argv.c -o ./dmalloc_argv.o
rm -f dmalloc
gcc -o aout dmalloc.o dmalloc_argv.o compat.o env.o \
-L.
mv aout dmalloc
rm -f libdmalloc.so libdmalloc.so.t
ld -shared --whole-archive -soname libdmalloc.so -o libdmalloc.so.t libdmalloc.a arg_check.o compat.o dmalloc_rand.o dmalloc_tab.o env.o heap.o chunk.o error.o malloc.o
ld: libdmalloc.a(arg_check.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
libdmalloc.a(arg_check.o): error adding symbols: Bad value
Makefile:259: recipe for target 'libdmalloc.so' failed
make: *** [libdmalloc.so] Error 1


Tuesday, January 12, 2016

dmalloc: compiling and installing a static library

dmallochttp://dmalloc.com/

Installation (on Fedora 21 x86_64)

$ uname -irs

Linux 3.18.3-201.fc21.x86_64 x86_64

$ tar -zxvf dmalloc-5.5.2.tgz
dmalloc-5.5.2]$ cd dmalloc-5.5.2/
dmalloc-5.5.2]$ ./configure
configure: configurations for the dmalloc library
configure: build utilities
checking for gcc... gcc
...
checking for strtok... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating conf.h
config.status: executing dmalloc.h.2 commands
config.status: executing settings.h commands
configure: 
configure: Please check-out Makefile and conf.h to make sure that
configure: sane configuration values were a result.
configure: 
configure: You may want to change values in settings.h before
configure: running 'make'.
configure: 
configure: To run the basic library tests, you can execute:
configure:   make light
configure: or
configure:   make heavy
configure:

dmalloc-5.5.2]$ make
rm -f dmalloc.h dmalloc.h.t
cat ./dmalloc.h.1 dmalloc.h.2 ./dmalloc.h.3 > dmalloc.h.t
mv dmalloc.h.t dmalloc.h
rm -f arg_check.o
gcc -g -O2  -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1  -I. -I.  -c arg_check.c -o ./arg_check.o
In file included from /usr/include/string.h:634:0,
                 from arg_check.c:33:
dmalloc.h:484:7: error: expected identifier or ‘(’ before ‘__extension__’
 char *strndup(const char *string, const DMALLOC_SIZE len);
       ^
Makefile:362: recipe for target 'arg_check.o' failed
make: *** [arg_check.o] Error 1

Thursday, May 14, 2015

Convert images to pdf and back

Today I found out a simple and flexible tool which converts images to pdf and vice versa - convert. It was already built in my Fedora 21.

Example:

Let's say I have 1.jpg, 2.jpg, 3.jpg files. To convert them into a single result.pdf run the following command:

$ convert 1.jpg 2.jpg 3.jpg result.pdf

or

$ convert {1..3}.jpg result.pdf

Now I want to get my images back from the result.pdf:

$ convert -density 300 result.pdf image.jpg

where "-density 300" is a precision in points per inch. Each pdf page will be converted into a separate image.

convert supports a lot of different flags and options. To learn more about it:

$ man convert

or

Wednesday, April 1, 2015

log4j2: line separator in header's parameter of PatternLayout

Log4j2 provides a new parameter for PatternLayout:

Parameter name: header
Type: String
Description: the optional header string to include at the top of each log file

With the following log4j2.xml configuration:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="20">
    <Appenders>
        <RollingRandomAccessFile name="file" fileName="logs/app.log" filePattern="logs/app-%d_%i.log.gz">
            <PatternLayout>
                <header>
Version: ${env:APP_RELEASE_VERSION}
                </header>
                <pattern>%message%n</pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy/>
            </Policies>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="file"/>
        </Root>
    </Loggers>
</Configuration>

Git: undo the last rebase

$ git reflog

f0d821f HEAD@{0}: commit_a
2ea24f8 HEAD@{1}: commit_b
0c3cd49 HEAD@{2}: commit_c
de5b02a HEAD@{3}: commit_d

Suppose that "commit_c" was the head commit of the branch just before the rebase. Let's reset the current branch to this commit:

$ git reset --hard HEAD@{2}