At home on the weekend, my girlfriend was checking wechat moments. Suddenly she asked me:

Hongmeng OS Review

On August 9, 2019, At huawei Developer Conference, Yu Chengdong, CEO of Huawei Consumer Business, officially announced the release of its own operating system Hongmeng, which has Linux kernel, Hongmeng Microkernel and LiteOS kernel. The future will be free of the Linux kernel and LiteOS, and only the microkernel.

Harmony OS (Ark) is a cross-platform operating system developed by Huawei since 2012 that may be compatible with Android App.

Figure: Four technical features of Hongmeng OS
1. Distributed architecture is first used in terminal OPERATING systems to achieve seamless cross-terminal collaborative experience
2. Determine the time delay engine and high-performance IPC technology to achieve natural fluency of the system
3. Rebuild the trusted security of terminal devices based on the microkernel architecture
4. Achieve cross-terminal ecological sharing through unified IDE to support one-time development and multi-terminal deployment

What is cross-platform

Previously, the platform ≈ operating system. Therefore, cross-platform in the traditional sense is independent of operating system and hardware environment. An application developed on one operating system can still run on another.

But with the development of science and technology, the platform ≈ operating system has not been established, just like The Hongmeng OS launched by Huawei, he can support a variety of equipment, such as mobile phones, watches, computers, cars, smart home equipment, etc..

So, when we talk about cross-platform today, we mean cross-device. Platform ≈ equipment

Therefore, Huawei hopes that Hongmeng OS can run on a variety of devices, so hongmeng OS must have cross-platform capability.

And it’s not just that the operating system can be cross-platform, but more importantly that users and developers feel truly cross-platform.

Therefore, the purpose of cross-platform operating system Hongmeng is to enable developers to focus on their own business logic and develop cross-terminal distributed applications as if they were developing the same terminal, and to enable final consumers to enjoy the seamless experience brought by powerful cross-terminal business collaboration for each use scenario.

Java cross-platform implementation

Let’s start with how Java is cross-platform.

Java’s support for cross-platform, like support for security and network mobility, is distributed throughout the Java architecture. Among them, the Java language specification, Class files, Java Virtual Machine (JVM) and so on play an important role.

First, the Java language specification specifies the value range and behavior of the basic data types in the Java language. Second, all Java files are compiled into a unified Class file. Finally, the Class files are converted into platform-appropriate binaries using the Java virtual machine.

The platform independence of Java builds on the platform independence of the Java Virtual machine because the Java Virtual machine hides underlying operating system and hardware differences.

To run a piece of Java code, you have to go through several steps to convert the Java source code into machine code that can be executed by the machine, a process that is largely done by the virtual machine.

In the popular HotSpot VIRTUAL machine, there are two main forms of explain execution and just-in-time compilation:

  • Explain to perform

    • Translate bytecode into machine code and execute it

  • Just-in-time (JIT) compilation

    • Compile all the bytecodes contained in a method into machine code before executing it.

HotSpot defaults to hybrid mode, combining the benefits of both explain execution and just-in-time compilation. It interprets the execution of bytecode, and then compiles the hot-spot code (hot-spot detection) that is executed repeatedly within it, on a method-by-method basis, in real time.

Android cross-platform

Android is actually based on the Java language, so similarly, to run a piece of Android code, you have to go through several steps to convert the Android source code into machine code that the machine can execute.

However, this conversion process is implemented differently in different versions of Android:

Android 1.0 (2008) : Uses a virtual machine called Dalvik with an integrated interpreter. When the App is running, this interpreter is called, explaining the code sentence by sentence, very slowly.

Android 2.2 (2010) : Introduced the Just-in-time (JIT) compilation mechanism. When an App is running, it compiles frequently used functions into machine code 010101 that the machine can execute directly, without translating them sentence by sentence. When unusual functions occur, the interpreter is called to translate them. This is faster, but every time you start your App, you have to recompile it.

Android 5.0 (October 2014) : Virtual machine Dalvik replaced with ART (Android Run Time), JIT compiler replaced with AOT (Ahead of Time). In this way, when the App is downloaded and installed on the phone, it first compiles the code that can be compiled into 101010 that the machine can understand. The rest of the code that doesn’t translate well wakes up the interpreter while the user is using it. This way, you don’t need to compile every time you open your App, but it takes a bit longer to install and takes up space on your phone.

Android 7.0 (2016) : The hybrid compilation mechanism is adopted. The intermediate code is not compiled at installation, but the part of the code that can be compiled into machine code when the user is idle is compiled statically through the AOT compiler. If the AOT has not yet compiled or cannot compile, then the JIT+ interpreter is called. This mechanism, like trading time for space, not only shortens the waiting time for users to install apps, but also maximizes the optimization that compilers and interpreters in virtual machines can do.

Android compilation issues

As you can see, Android has been working hard on compilation optimization since Android 1.0 in 2008.

The current Android model is a combination of explain execution + JIT + AOT, which has achieved a good balance in space usage + speed of installation + speed of execution.

But Android has been criticized for its compilation problems. Although the interpreter has been improved in subsequent Android 8.0, the efficiency of interpretation mode execution has been greatly improved; Android 10.0 provides a way to pre-place hotspot code so that an application knows when it is installed that frequently used code will be pre-compiled.

For now, however, Android has not been able to shake off the premise that applications are packaged as APK in Java code. In other words, APK has to go through a compilation process inside the Android system before it becomes user usable, which is a big hurdle to get around.

Hongmeng cross-platform implementation

So, how does hongmeng OS code compile? How does he solve the cross-platform problem?

As you can see from the figure above, ark compiler and multi-terminal development IDE play an important role in the Hongmeng OS architecture.

Cross-platform has the biggest challenge, that is, the adaptation of various platforms, especially at present, there are more and more types of devices, how to adapt the same application in mobile phones, watches, cars, TV display? This is what a multi-terminal development IDE does.

Use huawei terminals IDE, unified language compilation, distributed architecture Kit provides the screen layout and interaction of adaptive control, drag and drop support controls, preview oriented visual programming, so that developers can run automatically based on the same project effectively build multiterminal App, realize the real time development, deployment, and Implement a shared ecosystem across devices.

This is the IDE provided by Huawei, in which you can drag and drop controls through a graphical interface, and the IDE can help automatically adapt to various terminal devices.

With an IDE, it is easy to develop a set of code that can be automatically adapted to various devices, but the machine instructions executed by different devices are different. How do you compile this set of code into the machine instructions that each device needs?

Android devices are compiled by virtual machines built into different devices, so you know what the device is before you compile it. So, what does Hongmo OS do? That’s what the Ark compiler does.

Huawei Ark compiler is the first static compiler to replace Android VIRTUAL machine mode, allowing developers to compile high-level languages into machine code in the development environment at one time. In addition, ark compiler will support multi-language unified compilation in the future, which can greatly improve the development efficiency.

Android is “slow” because the compilation process takes place on the terminal, meaning that it needs to be compiled into executable machine code by the virtual machine on the user’s phone.

The Ark compiler used by Hongmun OS can convert high-level language (Java) directly into machine code, bypassing virtual machines. And this compilation process is not done on the user’s phone, but during the application development phase.

Through the ark compiler, the developer’s application in the download before they are converted to machine code that can be recognized, thus it can be fast installation, start-up and running on your phone, without the compilation of after the VM – to some extent, the ark advance compiler is the compilation process to application development, thus greatly reduce the burden of smartphones and the operation of the operating system.

According to Huawei’s official introduction, Ark compiler is the first static compiler to completely replace language VIRTUAL machine, and does not need an interpreter at all. Java development efficiency and C language efficiency compiler.

In addition to code compilation, ark compiler also provides a more efficient memory mechanism, which differs from Android memory reclamation in the following:

Android uses a centralized collection mechanism for memory reclamation, which makes it more necessary to suspend applications for global reclamation. This is also one of the root causes of random lag. Ark compiler uses reference counting method to recycle memory in real time, and uses special elimination loop algorithm to avoid the system lag caused by GC centralized collection. Compared to GC, ark’s memory reclamation is real-time rather than centralized and there is no need to suspend the application process, which greatly eliminates the lag.

And just as the JVM actually supports multiple languages, Huawei says the Ark compiler will also support more development languages in the future. In other words, developers in other languages will be able to develop applications based on The Hongmeng OS.

References:


https://www.jishuwen.com/d/2NN3

https://www.zhihu.com/question/339567108 https://www.cnbeta.com/articles/tech/876171.htm https://www.cnbeta.com/articles/tech/876919.htm https://juejin.cn/post/6844903817637691400