Skip to content

Discussion: Cache Class Chain in the J9Class struct #15013

Closed

Description

@dsouzai

@JamesKingdon is working on a customer issue where an AOT body is loading a field that's off by 4 bytes. After digging through the coredump and the code, I think I found what the problem is.

There are two class hierarchies:

  1. B extends A
  2. B extends A'

The two Bs are loaded by different classloaders, but have the same J9ROMClass. A and A' have the same name, but have a different number of fields, and as such have different J9ROMClasses.

A few pieces of background:

  1. When a class chain of a class is stored in the SCC, the key is derived from the J9ROMClass of the class
  2. As per
    chainData = findChainForClass(clazz, key, keyLength);
    if (chainData != NULL)
    {
    LOG(1, "\tchain exists (%p) so nothing to store\n", chainData);
    return chainData;
    }

    if a chain already exists in the SCC, the JIT assumes it applies for the J9Class that's passed in.
  3. If it was possible to generate class chains for both Bs, they would be different.

Given that, it is possible for the following to occur (in the order specified) :

  1. In the compile run, we generate a class chain for the 2nd B.
  2. In the compile run, we go to compile the 1st B's foo. As part of the pre compilation tasks, we try to store the class chain at:
    // Ensure we can generate a class chain for the class of the
    // method to be compiled
    && (NULL != fe->sharedCache()->rememberClass(J9_CLASS_FROM_METHOD(method)))

    Because it uses the 1st B's J9ROMClass, which is the same as the 2nd B's J9ROMClass, we see that it already exists. So we compile and store the compiled method body into the SCC, using the J9ROMMethod as the key - it is important to note that because both Bs have the same J9ROMClass, the 2nd B's foo's J9ROMMethod is also the same as the 1st B's foo's J9ROMMethod.
  3. In the load run, we go to compile the 2nd B's foo. However, we see that a body exists in the SCC., so we go to load it. First we validate the class chain as per:
    // Validate the class chain of the class of the method being AOT loaded
    if (*aotCachedMethod && !fe->sharedCache()->classMatchesCachedVersion(J9_CLASS_FROM_METHOD(method)))

    which succeeds because we're comparing the 2nd B's class chain with itself, so the AOT load continues.

We end up with a situation where we compiled a body that's only expected to work with the 1st B, but we're executing it with the 2nd B. What's crazier is that because we only validate the class chain in the load run, if we were to try and load AOT code for the 1st B's foo, it would fail because there would be a class chain mismatch.


The problem boils down to the fact that rememberClass assumes that if a class chain is found for a class, it is valid for that class. However, just because two classes have the same J9ROMClass does not mean that they necessarily have the same class chain.

One way to solve this is to have rememberClass always generate a class chain, and then if there exists one in the SCC, we compare both to ensure that they're the same before returning it to the caller. However, this has the potential of increasing compilation time.

The solution that I'm proposing is to add a field to the J9Class (and J9ArrayClass) struct(s). This ensures that we build and validate the class chain at least once for each J9Class, but we don't have to build and validate it for every call to rememberClass. However, I don't know what subtleties are involved when it comes to adding such a field to the J9Class (e.g. do we have code that assumes a certain fixed size, HCR considerations, etc.). As such, I'm opening this issue to discuss the proposed solution as well as any other ideas for dealing with this problem.

Activity

  1. dsouzai commented on May 5, 2022

    @dsouzai
    ContributorAuthor

    @DanHeidinga @tajila @babsingh @mstoodle @mpirvu I'd appreciate your thoughts on this.

  2. mpirvu commented on May 5, 2022

    @mpirvu
    Contributor

    The first solution, rememberClass always generating a class chain, may increase AOT compilation time. Does it have any effect on AOT loads?

  3. babsingh commented on May 5, 2022

    @babsingh
    Contributor

    ++ @hangshao0 @gacholio for insights.

  4. vijaysun-omr commented on May 5, 2022

    @vijaysun-omr
    Contributor

    Re: "One way to solve this is to have rememberClass always generate a class chain, and then if there exists one in the SCC, we compare both to ensure that they're the same before returning it to the caller. However, this has the potential of increasing compilation time."

    I doubt that this increase in compile time will be significant; usually the profile is dominated by other far more expensive passes like global register allocation, global value propagation etc.

  5. dsouzai commented on May 5, 2022

    @dsouzai
    ContributorAuthor

    Does it have any effect on AOT loads?

    I don't think so; I don't believe we have much need to generate class chains during an AOT load.

  6. hangshao0 commented on May 5, 2022

    @hangshao0
    Contributor

    The two Bs are loaded by different classloaders

    Seems the root is 2 different class loaders, and there is no class loader info involved in the whole process. Not sure how hard it would be to add class loader info here.

  7. gacholio commented on May 5, 2022

    @gacholio
    Contributor

    Classes are always uniquely identified by the combination of name and defining class loader. In days gone by, this would uniquely map to a ROM class, but that's no longer the case.

    Does the chain generation/verification somehow take class loaders into account?

  8. dsouzai commented on May 5, 2022

    @dsouzai
    ContributorAuthor

    Class Loaders ultimately are Java Objects, and as such, are transient from an AOT point. The class chain is a JVM agnostic way of representing a class hierarchy. It is essentially an array of J9ROMClasses (technically offsets into the SCC) of the J9Classes of the class, super classes, and interface classes that make up the transient J9Class.

    Does the chain generation/verification somehow take class loaders into account?

    The class chain is only one part of the validation; it essentially is used to ensure that the "shape" of a class that was assumed in the compile run is maintained in the load run. For example, ensuring that there are the same number of fields and that they're in the same order, and so on. The remaining validations and relocation records are what ensure that the compiled method body can be executed in another JVM instance (see the aot validation blog post for how this happens).

    Seems the root is 2 different class loaders, and there is no class loader info involved in the whole process.

    If several J9Classes can have the same J9ROMClass, then they can also share the same class chain. So in that case, there can be multiple classloaders, but they'll all be loading essentially equivalent classes, and so it's possible for the same body to be loaded multiple times. However, we cannot guarantee that the chain will be same (as manifested by the root problem). Because a class chain is keyed by the J9ROMClass, we can't store two different class chains against the same key. That is the root cause of the problem.

  9. gacholio commented on May 6, 2022

    @gacholio
    Contributor

    If several J9Classes can have the same J9ROMClass, then they can also share the same class chain.

    I'm not sure that's true. It's entirely possible that a ROM class is used in different loaders with different superclasses (and subclasses for that matter). All that has to match is the name.

  10. dsouzai commented on May 6, 2022

    @dsouzai
    ContributorAuthor

    If several J9Classes can have the same J9ROMClass, then they can also share the same class chain.

    I'm not sure that's true. It's entirely possible that a ROM class is used in different loaders with different superclasses (and subclasses for that matter). All that has to match is the name.

    Right, that's what's happening in the root issue. My point was to highlight the fact in the case where two class loaders load the same class from disk, then though we end up with two different J9Classes, they'll have the same class chain. Therefore, it is possible (though not guaranteed) that AOT code in the SCC generated by compiling one class' bytecodes can be used for the other (after appropriate relocation).

    The problem comes from the fact that, as you say, you can have different superclasses. In that case, they would have different class chains, but at the moment, we're only able to store one of them because the key is derived from the J9ROMClass of the most derived class (which can be the same).

  11. 3 remaining items

  12. mstoodle commented on May 6, 2022

    @mstoodle
    Contributor

    Could this also be solved by having findChainForClass validate any class chain it finds for the class it's currently looking at? Then you would detect that you shouldn't use the class chain for the second B when you're compiling the first B's foo ? And isn't that really where you start going wrong?

  13. mstoodle commented on May 6, 2022

    @mstoodle
    Contributor

    oh, I guess that's the proposed solution which may increase compilation time (sorry, stopped reading too early :) ). Note that it shouldn't have to generate a chain, it can just walk the chain to validate it against the current class (both of which you have in your hands already...). I think that avoids having to store any chains on the J9Class...

  14. dsouzai commented on May 6, 2022

    @dsouzai
    ContributorAuthor

    Note that it shouldn't have to generate a chain, it can just walk the chain to validate it against the current class...

    Yeah, bad choice of words on my part, I was always planning on just using classMatchesCachedVersion.

  15. babsingh commented on May 6, 2022

    @babsingh
    Contributor

    However, I don't know what subtleties are involved when it comes to adding such a field to the J9Class (e.g. do we have code that assumes a certain fixed size, HCR considerations, etc.)

    If generating the class chain always is not efficient and a RAM class field is needed to cache the class chain, no subtleties are involved. One just needs to add the field in J9Class (at the end), and set it. RAM class creation code will initialize it to 0. I don't think a huge impact on footprint should be seen with the addition of the field. But, the impact on footprint should be evaluated if this approach is taken.

  16. gacholio commented on May 6, 2022

    @gacholio
    Contributor

    What is in the class chain? Is it RAM or ROM classes (or something else)?

  17. mstoodle commented on May 6, 2022

    @mstoodle
    Contributor

    What is in the class chain? Is it RAM or ROM classes (or something else)?

    It's a list of offsets to rom classes in the shared cache. If a rom class isn't stored in the shared cache, you cannot build the class chain. it's built by walking the superclasses, then walking the itables to add the implemented interfaces.

  18. babsingh commented on May 6, 2022

    @babsingh
    Contributor

    What is in the class chain? Is it RAM or ROM classes (or something else)?

    A detailed description is also available in https://github.com/eclipse-openj9/openj9/blob/master/doc/compiler/aot/ClassChains.md#class-chains.

  19. gacholio commented on May 6, 2022

    @gacholio
    Contributor

    Here's the scenario I was considering:

    ClassLoader1
      A <- ROM class A1
        B <- shared ROM class B
    
    ClassLoader2
       A <- ROM class A2
        B <- shared ROM class B
    

    If the RAM classes are used for the walk described above, I think we'll be OK.

  20. dsouzai commented on May 6, 2022

    @dsouzai
    ContributorAuthor

    We can't use RAM classes because they're transient from an AOT point of view; there isn't a direct way for two JVMs to agree on whether the RAM Class B they see are the same, because a J9Class is a per-JVM instance.

    The class chain is a way of dealing with this, by basically being one part of the validation that allow two JVMs to agree on whether they're seeing the same class.

    In the scenario you're describing (which btw, is exactly the scenario mentioned in this issue's desc), we would need two class chains. However, we store the class chain using the a key that's derived from the shared ROM class B. As such, we're in a situation where we have two different class chains but only one key. This ultimately is the shortcoming that can lead to the root problem.

  21. gacholio commented on May 6, 2022

    @gacholio
    Contributor

    From Mark's comment, the walk must be performed using the RAM classes (otherwise there would be no iTables to walk). If the class chains are stored in a new RAM class field, then that disambiguates the cases doesn't it?

  22. dsouzai commented on May 6, 2022

    @dsouzai
    ContributorAuthor

    From Mark's comment, the walk must be performed using the RAM classes (otherwise there would be no iTables to walk).

    Yeah, so the way it works is you take a RAM class and use that to build a class chain, and store it into the SCC. Then in another JVM instance, you take the class chain from the SCC and compare it to the RAM class. So you're never directly comparing RAM class to RAM class (because you can't between JVM instances).

    If the class chains are stored in a new RAM class field, then that disambiguates the cases doesn't it?

    The class chain needs to be stored into the SCC, because it is the means of communication between two JVM instances. So when I was talking about storing it into a new field, I meant that I would be caching the class chain pointer (which is a pointer into the SCC) in the field.

    In your example above, I can only cache a class chain for ClassLoader2's B or ClassLoader1's B, because I can only store one of the class chains into the SCC. The reason is because the key we use to store the data is an ASCII representation of the offset into the SCC of B's J9ROMClass:

    uintptr_t classOffsetInCache;
    if (!isROMClassInSharedCache(romClass, &classOffsetInCache))
    {
    LOG(1,"\trom class not in shared cache, returning\n");
    return NULL;
    }

    createClassKey(classOffsetInCache, key, keyLength);

    TR_J9SharedCache::createClassKey(UDATA classOffsetInCache, char *key, uint32_t & keyLength)
    {
    keyLength = _numDigitsForCacheOffsets;
    convertUnsignedOffsetToASCII(classOffsetInCache, key);
    }

    chainData = (uintptr_t *)sharedCacheConfig()->storeSharedData(vmThread, key, keyLength, &dataDescriptor);

    So if we can store two different class chains into the SCC that start with a common J9ROMClass, then yeah, we can load both and put them in the fields of the two different J9Class. However, as it stands, if we were to use the field, one of the J9Classes would have the class chain cached, and the other would have to have a -1 or something like that to represent the fact that there is no class chain for it, and therefore, any AOT compilation should continue as it has no information about that class (i.e. unresolved).

  23. dsouzai commented on May 10, 2022

    @dsouzai
    ContributorAuthor

    I ran Liberty startup with DT7, with forceAOT and a big SCC to get a decent number of methods AOT compiled. I only ran cold runs (ie, destroyed the SCC every time). There were ~7k methods AOT compiled. I didn't see a measurable increase in the compilation time, and this was with a change where I didn't cache the fact that the J9Class had already been successfully remembered.

    As such, I think we can just go ahead with the simple solution of always calling classMatchesCachedVersion in rememberClass if a class chain already exists in the SCC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions