runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Jitted Code Pitching Support

Open sergign60 opened this issue 10 years ago • 22 comments

We propose to support releasing memory that is allocated for methods that have been executed and will never be executed again or can be recompiled. That is needed for devices with limited memory capacity.

Also when GDB JIT feature (#6278) is enabled there is additional memory consumption (~700 bytes on ARM32 per method with several lines) for ELF debugging information. Such memory can be released when jitted code for method itself released.

The existing execution model of coreclr without releasing memory for executed code and debugging info creates serious obstacles for applications running on devices with limited memory or even can make impossible their execution in certain cases.

@papaslavik @brucehoult @Dmitri-Botcharnikov @leemgs @lemmaa @wateret

sergign60 avatar Aug 16 '16 13:08 sergign60

The earlier versions of CLR had this mechanism. It was called code-pitching but it was removed because of it was very unreliable. The code for it was part of "Shared Source Common Language Infrastructure" that you can find around if you would like to take a look. The hard problem with code pitching is to guarantee that the method being discarder is not executing, and that nobody is holding pointer to it.

jkotas avatar Aug 16 '16 13:08 jkotas

cc @noahfalk @dotnet/jit-contrib

jkotas avatar Aug 16 '16 13:08 jkotas

Is it possible to define that the method is not executing by stopping the program execution and inspection of stack frames for all threads?

sergign60 avatar Aug 16 '16 14:08 sergign60

Yes, the original code pitching implementation have done something similar.

jkotas avatar Aug 16 '16 14:08 jkotas

except for FCALLs? Because I read in comments // FCall is a high-performance alternative to ECall. Unlike ECall, FCall // methods do not necessarily create a frame. Jitted code calls directly // to the FCall entry point. It is possible to do operations that need // to have a frame within an FCall, you need to manually set up the frame // before you do such operations.

sergign60 avatar Aug 16 '16 14:08 sergign60

FCalls are a problem because they are implemented in C++ or hand-written assembly, and so they cannot be discarder.

jkotas avatar Aug 16 '16 14:08 jkotas

needed for devices with limited memory capacity

It may be a good idea to do analysis on how much memory can be saved by doing this for the typical applications. The JITed code does not typically dominate the working set.

jkotas avatar Aug 16 '16 14:08 jkotas

For data we have garbage collection... So it sort of has a control on the memory increase for data.

@jkotas I wonder if you mean the meta-data more than just data compared to the code here...

papaslavik avatar Aug 16 '16 14:08 papaslavik

Also it's interesting if it's possible to write a test using serialization an de-serialization or something like that which causes an unbounded increase in the memory for the code of dynamic methods.

papaslavik avatar Aug 16 '16 14:08 papaslavik

The hard problem with code pitching is to guarantee that the method being discarder is not executing, and that nobody is holding pointer to it.

Do you mean the backpatching thing here or arbitrary pointers to the code?

papaslavik avatar Aug 16 '16 14:08 papaslavik

I also believe such a feature could be useful for the application of an aggressive inlining etc.

papaslavik avatar Aug 16 '16 15:08 papaslavik

@jkotas From my impression on average the JITed code size of a small function on ARM is 3 times larger than the corresponding IL assembly of the method (not talking about too small functions).

Of course, the functions which have a lot of calls interfered with the local variables might have the factor of 8 or similar.

papaslavik avatar Aug 16 '16 15:08 papaslavik

E.g. if we take a test JIT/SIMD/CircleInConvex_r/CircleInConvex_r.exe:

Method ClassLibrary.test:convex_hull(ref) has 476 IL size and 1426 emitted size ClassLibrary.test:Check(ref):bool : 166 IL and 570 emitted A small function ClassLibrary.test:abs(float):float has 19 IL vs 114 emitted

Also the method Main() for the test IT/Directed/cmov/Float_Xor_Op_cs_d/Float_Xor_Op_cs_d.exe has a bigger factor: 3763 IL vs 33234 emitted

So, I believe there might be a considerably big application which has a lot of executed code.

papaslavik avatar Aug 16 '16 15:08 papaslavik

First, I do agree that having ability to drop JITed code would be really nice.

Based on my experience, I do not believe that it will move the needle significantly (e.g. more than 1.5x) in total RAM consumption of managed process, or in ability to run on very low end devices, for typical apps.

jkotas avatar Aug 16 '16 15:08 jkotas

But if you had to have a Facebook and a Browser application on a low-end device, it would be a non-typical but a very resource consuming app.

papaslavik avatar Aug 16 '16 16:08 papaslavik

Still Facebook presumably would have a lot of class description meta-data present in the RAM. So it would nevertheless be about 1.5x as you say, but the absolute number could be significant.

papaslavik avatar Aug 16 '16 16:08 papaslavik

@jkotas By the way, is the class unloading feature present in coreclr?

Well, I don't think it would be, provided, no code unloading is supported.

papaslavik avatar Aug 16 '16 16:08 papaslavik

Class unloading is present for Reflection.Emit (AssemblyBuilderAccess.RunAndCollect). It works with assembly granularity.

jkotas avatar Aug 16 '16 17:08 jkotas

Method ClassLibrary.test:convex_hull(ref) has 476 IL size and 1426 emitted size

Out of interest, I quickly converted convex_hull(List a) to C++ by adding my own C#-like declarations for Point and List and making a, up, down references (fewer syntax changes than making them pointers). clang++ -Os compiled it to 0x27a (634) bytes of Thumb2 armhf code. Note: cw() and ccw() were both fully inlined. Making all the functions virtual increased it to 0x2d6 (726) bytes.

struct Point {float X; float Y;};

template <class T>
class List{
public:
  List();
  int Count;
  void Add(T);
  void RemoveAt(int);
  void Clear();
  T& operator[](int i);
  T& Last();
  typedef int cmp_fn(T&, T&);
  void Sort(cmp_fn);
};

brucehoult avatar Aug 16 '16 18:08 brucehoult

@sergign60 Is this still an active issue tracking work being done?

gkhanna79 avatar Mar 06 '17 04:03 gkhanna79