WSL: fix Vmmem high CPU usage

Last time, when I wrote about using a trackpad instead of a mouse, I mentioned also about my migration from MacOS to Windows. Right now, I work with Windows and use WSL (Windows Subsystem for Linux) every day. It’s a great tool which I will describe in future posts, but today just want to share a quick info – sometimes when we use WSL we can observe high CPU usage by Vmmem process. Without a clear reason, without any CPU-intensive operations inside our WSL. In such case you can open a WSL terminal, run top or htop command and check CPU usage – probably it is caused by init process inside your Linux distribution.

The problem is known and Microsoft works to resolve it, but right now we have to use one of possible solutions. First one is just to restart WSL distribution – it will help and after reopen, Vmmem CPU usage should be normal. You can restart WSL using this simple command from PowerShell / Windows Terminal:

wsl --shutdown

Yes, a little strange, because it says “shutdown”, and in reality, it will restart your WSL. This should resolve issue, but it can happen again – many users, including me, report that high CPU usage occurs after system sleep. I had similar issues with Docker for Desktop on Mac, it slowed down dramatically after sleep and wake up. The only solution to fix that was restarting Docker.

With WSL we can use small trick – disable Windows Subsystem for Linux GUI. Many users report, that the issue is caused by that module, even if we do not use any GUI apps from Linux inside our WSL. I do not use them, so decided to disable WSLg. The thing is more interesting – WSLg is not available in current stable Windows 10 revision (19043), but probably already implemented and can cause some issues. To disable WSLg we have to edit .wslconfig file. In default configuration, it isn’t present, so you have to create it manually in: systemdrive:/Users/your-username/.wslconfig(yes, with the dot in the beginning). Then add to that file simple configuration:

[wsl2]
guiApplications=false

Save file and restart the computer or just restart WSL using command above. It should resolve that issue and after they will fix it in WSL core, it will be possible to unlock GUI apps. By the way, you can configure different WSL settings in that file like maximum memory usage limit – just check official Microsoft documentation.

Why and how use Laravel Resources

Laravel is a pretty nice PHP framework and provides a lot of useful features. One of them is Resource class. Very often we get some data from an ex. database and send it to our app client. Sending the whole model is very bad option and there are many reasons for that:

  • we probably do not want to disclose our models structure
  • some data may be confidential – maybe our client will not use that fields, but every user will be able to look at request, response and get that data
  • many clients (ex. mobile devices) do not need all the data

Without Resource class we have to create our own class and make some transformations, prepare data from a database for clients. If we decide to use a built-in Resource, it will be much, much simpler. We only have to return such class instances and pass our data into the constructor, and then write what fields we really want to use. There is simple example of Laravel Resource which will prepare our data to send as a JSON:

declare(strict_types=1);

namespace App\Modules\MyModule\Resources;

use Illuminate\Http\Resources\Json\Resource;

class MyModelResource extends Resource
{
   /**
    * @var MyModel
    */
   public $resource;

   public function toArray($request)
   {
      return [
       “id” => $this->resource->getKey(),
       “name” => $this->resource->my_model_name,
       “count” => $this->resource->my_model_attribute,
       (...)
      ];
   }

}

That comment with $var MyModel is optional, but helps some IDEs recognize what model will we use inside our Resource. And there is usage in our controller:

public function getModel(Request $request): JsonResponse
{
   // here some code for get model (or inject it with route)
   $model = MyModel::find($request->id);   

   return response()->json(
       new MyModelResource($model)
   );
}

Simple, clean and elegant, because we transform our model in a separate place. If it’s required, we can modify our data in a more complex way, no problem with that. We can also use… Resource inside Resource, so it’s ok to make something like that:

public function toArray($request)
{
    return [
        "id" => $this->resource->getKey(),
        "name" => $this->resource->my_model_name,
        "relation" => new MyModelRelationResource($this->resource->relation),
        (...)
  ];
}

Collection Resources

What if we have a lot of items and want to send them as a JSON? It’s also not a problem, because we can use a built-in ResourceCollection class to achieve that. There is another example:

declare(strict_types=1);

namespace App\Modules\MyModule\Resources;


use Illuminate\Http\Resources\Json\ResourceCollection;

class MyModuleCollectionResource extends ResourceCollection
{
   public function toArray($request)
   {
       return MyModuleResource::collection($this->collection);
   }
}

Of course we can modify or transform the collection in the same way as in a normal Resource, just a response array. Usage in controller:

public function getModels(Request $request): JsonResponse
{
   $models = MyModel::all();   

   return response()->json(
       new MyModuleCollectionResource($models)
   );
}

As you can see, it’s very easy to use, allows us to make everything more organized and solve a lot of issues. 

Using Magic Trackpad instead of mouse

I have not added anything new on my blog for a long time, but changes are coming and it will be much better, I promise. Today I want to share my experiences related to moving from using a mouse to touchpad only. For a lot of people it may be something strange – it was strange for me when I decided to try that – but in reality, it has some interesting advantages. Of course not all usage scenarios are good to do something like that and not all hardware will be good enough to make such transition in an efficient way. 

First, the reasons. If you are a reader of my blog you know, I like minimalism in many, many various forms. We have used computers with mice for many years, but they always have some issues – the mouse must be complicated to be precise, must contain additional buttons and a wheel to provide comfort and flexible usage. All these things increase the chance of mechanical damage. I damaged a few mouses in my life, most of them because of wearing contact-sticks under the buttons. After that, accidental double-clicking or no-clicking happened. In some cases it’s pretty easy to self-repair, in some it isn’t, but I know one thing: it will happen. Always, it’s just a matter of time: if you use your mice often, buttons will stop working correctly someday. It’s weird that usually it happens after the guarantee period. 

Second thing is related to my health. For a few years I have had problems with trigger finger. The reason is not clear, but it may be related to the mouse and I want to try something else. I work with a computer a few hours each day and it can cause such symptoms. I’ve already got some injections, but they helped only for a limited time, like a year. When we use a trackpad, we use our hands and fingers in different ways, so it was supposed to be an experiment and a way to avoid surgery. I use Macbook Pro so I decided to buy a Magic Trackpad (v2). It’s almost identical to build-in Mac trackpad, it supports Force Touch, so it should help me in many ways: first, it can integrate with MacOS in perfect way, second, if I decided to use Mac outside, in train or something like that, my feelings and experiences should be very similar / almost identical to use a computer in home with external Magic Trackpad. Does it work? Yes, it works and it’s great.

First of all, gestures. It’s like on Apple ads: “amazing”. Yeah, amazing how comfortable trackpad gestures are. Almost all built-in gestures are very helpful in daily work. For example I use virtual desktops on separate screens all the time. With the mouse I had to bind some buttons to switch desktops, right now it’s just a three fingers swipe left or right and it’s done. Fingers up, and I have all apps windows visible and can switch between them easily. Zoom is also great and helps in many situations, it’s much, much better than zooming pages by percentages using keyboard shortcut. With a trackpad, it’s smooth, without steps which can limit us. 

Device is very fun to use – the surface is glass and great in touching and swiping. As I wrote before, moving to the trackpad should help me using MacBook during trips and it’s true. Right now I use the built-in trackpad every time, it’s very comfortable and I know exactly what and how I can achieve it. It’s great to not feel the difference when we make some changes in the environment, because we do not have to waste time to find good solutions. It just works. 

Of course a trackpad will be a very bad option in some situations. I do not like to select multiple files using that device – my habit is to click, select some area and release the button, like on a mouse. On the trackpad that operation is quite difficult and not comfortable. Also, just try to use things like Google Maps or Komoot with a trackpad… it’s possible, but difficult and frustrating – zoom gesture helps a lot, but moving by hand dragging on the screen is a real nightmare. If you need very big precision, a trackpad is definitely not for you and you should stick with a mouse. 

One additional bad thing, but it’s related exactly to Apple Magic Trackpad, probably not all similar devices: battery life. I thought it was better. It’s not bad, I have to charge the trackpad every ~2 weeks, but I thought it would be much better. My last Logitech mouse needs charging every… I really do not remember, 3 months? Something like that with very active usage. It’s like day and night and it’s strange for me, because in mice we have more mechanical elements, Magic Trackpad is also pretty big and should have better battery life. It’s not a big deal, but I have to mention that to be 100% candid.

I do not understand why Apple still uses the Lightning port to charge the Magic Trackpad. There is a USB-A to Lightning cable in the box but… MacBooks haven’t had any USB-A ports for a long time! It’s so stupid and we can observe similar situation in many other places: iPhone, some iPads, Magic Mouse. Completely mess and not user-friendly, not Apple-style. Another thing is about my finger issue: after a few months I’m not sure, but probably, it can’t help me. Using a Magic Trackpad all day is comfortable, but it didn’t resolve my issues, so I have to visit a doctor and probably I’m going to have an operation. 

Overall I think it was a good decision to try Magic Trackpad. It’s a very nice device, but pretty expensive in comparison to high-end mices and has some limitations. Right now I’m migrating to… Windows machine, so probably will also go back to using a mouse instead of a trackpad all the time. Of course you will find the next post about why I decided to migrate and a lot of reasons for this. 

Moving to 1x drivetrain

1x drivetrain

I’ve used Planet X XLS bike for about 5 years and can say that is an amazing bike – I treat it not only as cyclocross, but also as road bike and gravel, depending on used tires. I started a few MTB races on this bike, I made my longest ride on this bike, I started using tubeless, tested a lot of different configurations… and changed a lot of things regularly. In the original specification, it was a 2x10s bike, but I changed it to 2x11s and now it is a 1x11s bike. I would like to write why I decided to move from 2×11 to 1x and why it is the best choice in my opinion. 

Continue reading “Moving to 1x drivetrain”

Say goodbye to Google Analytics

This time it will be really small announcement: I decided to remove Google Analytics from my blog. It allowed me to show visitors info, but in reality, I’ve never need such data. The price was high: Google was able to collect information about my visitors… Now think, it’s too big price and will not use such tool anymore, your privacy is more important than any charts in Google apps.