Skip to contentSkip to conversation

sh : performance problem

Qemu-devel Shin-ichiro KAWASAKI 4 participants 20 messages Feb 26, 2009 – Mar 4, 2009

lore
Share & tools
lore
#0 lore raw

Hi, all.

One of the current problems of qemu-sh4 system emulation is performance. Kernel boot process seems not so slow, but userland process performance is too bad. This problem can be seen numerically when you compile simple '.c' source with empty main(). The result is as follows.

sh4 : 5.8 [seconds] (-M r2d + Fedora 6)
arm : 0.8 [seconds] (-M versatilepb + Debian ARM)

sh4 is 7 times slower than arm...

/* I repeated the compile work until the measured time converges. */

Using Oprofile, we can see what qemu doing while the compile work on sh4 system emulation. Top 20 time consuming functions are as follows.

---------------------------------------------------------------------------
CPU: Core 2, speed 1600 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (Unhalted core cycles) count 100000

samples  %        image name               app name                 symbol name
38481    25.0550  qemu-system-sh4          qemu-system-sh4          find_tlb_entry
19836    12.9152  anon (tgid:2483 range:0xb2181000-0xb3381000) qemu-system-sh4          (no symbols)
9333      6.0767  qemu-system-sh4          qemu-system-sh4          cpu_sh4_exec
8180      5.3260  qemu-system-sh4          qemu-system-sh4          tcg_reg_alloc_op
7236      4.7114  qemu-system-sh4          qemu-system-sh4          temp_save
7051      4.5909  qemu-system-sh4          qemu-system-sh4          tcg_liveness_analysis
4339      2.8251  libc-2.8.90.so           libc-2.8.90.so           (no symbols)
3414      2.2229  qemu-system-sh4          qemu-system-sh4          tlb_set_page_exec
3017      1.9644  qemu-system-sh4          qemu-system-sh4          decode_opc
2802      1.8244  qemu-system-sh4          qemu-system-sh4          get_physical_address
sa2358      1.5353  qemu-system-sh4          qemu-system-sh4          find_itlb_entry
2215      1.4422  qemu-system-sh4          qemu-system-sh4          cpu_sh4_write_mmaped_utlb_addr
1771      1.1531  qemu-system-sh4          qemu-system-sh4          tcg_gen_code_search_pc
1746      1.1368  qemu-system-sh4          qemu-system-sh4          __ldl_mmu
1663      1.0828  qemu-system-sh4          qemu-system-sh4          cpu_sh4_handle_mmu_fault
1557      1.0138  qemu-system-sh4          qemu-system-sh4          gen_intermediate_code_pc
1328      0.8647  qemu-system-sh4          qemu-system-sh4          tcg_temp_new_internal_i32
1233      0.8028  qemu-system-arm          qemu-system-arm          cpu_arm_exec
1229      0.8002  vmlinux                  vmlinux                  vmi_activate
1046      0.6811  Xvnc4                    Xvnc4                    (no symbols)

---------------------------------------------------------------------------

Most time consuming function is 'find_tlb_entry()', which search the sh4's TLB entries to find an entry which matches with given address.
If my understanding is right, this search happens when TLB miss happens. Too many TLB misses causes bad perfomance, I guess.

The actions to solve this problem will be,

(i) tune up 'find_tlb_entry()'
(ii) reduce TLB miss by expanding page size
(iii) reduce TLB miss by increase the number of TLB entries virtually,
more than the real cpu has.

First, I tried (i). The attached patch introduces binary search. It shortens the gcc compile time from 5.8 seconds to 4.6 seconds :
it make sh4 system emulation 20% faster.

'find_tlb_entry()' searches unified tlb array with 64 entries, and instruction tlb array with 4 entries. This patch focus only on unified tlb array search.

This patch is rather rough one. Any advise to brush it up will be appreciated. I'm going to work on approaches (ii) and (iii). Advises for them will be thanked too.

Regards,
Shin-ichiro KAWASAKI

target-sh4/cpu.h +4 -1
Index: trunk/target-sh4/cpu.h
===================================================================
@@ -137,13 +137,16 @@
     uint32_t intevt;		/* interrupt event register */
 
     uint32_t pvr;		/* Processor Version Register */
     uint32_t prr;		/* Processor Revision Register */
     uint32_t cvr;		/* Cache Version Register */
 
-     CPU_COMMON tlb_t utlb[UTLB_SIZE];	/* unified translation table */
+    CPU_COMMON
+    tlb_t utlb[UTLB_SIZE];	/* unified translation table */
+    tlb_t * sorted_utlb[UTLB_SIZE];
+    uint32_t sorted_utlb_num;
     tlb_t itlb[ITLB_SIZE];	/* instruction translation table */
     void *intc_handle;
     int intr_at_halt;		/* SR_BL ignored during sleep */
 } CPUSH4State;
 
 CPUSH4State *cpu_sh4_init(const char *cpu_model);
target-sh4/helper.c +125 -4

179 lines not on this page · raw message

#2 lore raw

Hi, Lionel.
Thank you for the response!

Quoted 10 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Regards,
Shin-ichiro KAWASAKI

#3 lore raw
Quoted 9 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 5 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

Quoted 8 lines
Quoted 5 lines

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Yes, I will.

Do you know when the qemu-sh patches will be integrated to qemu ?

Signature · 1 line
Lionel Landwerlin <[email protected]>
#4 lore raw
Quoted 14 lines · Lionel Landwerlin
Quoted 13 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 9 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Yes, I will.

Do you know when the qemu-sh patches will be integrated to qemu ?

Nope.

Sometimes the main developers afford some time for sh kindly, then some of the patches in the qemu-sh staging repository are already integrated. But some patches are left. I guess they are busy on other CPU archs.

Especially, it's not a good time to apply patches which is not so serious, because version cutting work is going now.

Regards,
Shin-ichiro KAWASAKI

#5 lore raw
Quoted 30 lines · Shin-ichiro KAWASAKI
Quoted 14 lines · Lionel Landwerlin
Quoted 13 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 9 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Yes, I will.

Do you know when the qemu-sh patches will be integrated to qemu ?

Nope.

Sometimes the main developers afford some time for sh kindly, then some of the patches in the qemu-sh staging repository are already integrated. But some patches are left. I guess they are busy on other CPU archs.

Quoted 7 lines

Especially, it's not a good time to apply patches which is not so serious, because version cutting work is going now.

Regards,
Shin-ichiro KAWASAKI

There is also one thing that is annoying me.

With the algorithm you proposed in your patch, you're not able anymore to raise the MMU_ITLB_MULTIPLE and MMU_DTLB_MULTIPLE exceptions. This makes the MMU emulation different from the real processor (even if thoses cases aren't even handled in Linux and probably others OS).

For example you can probably not map the same physical area with differents virtual page sizes.

Signature · 1 line
Lionel Landwerlin <[email protected]>
#6 lore raw
Quoted 39 lines · Lionel Landwerlin
Quoted 28 lines · Shin-ichiro KAWASAKI
Quoted 14 lines · Lionel Landwerlin
Quoted 13 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 9 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Yes, I will.

Do you know when the qemu-sh patches will be integrated to qemu ?

Nope.

Sometimes the main developers afford some time for sh kindly, then some of the patches in the qemu-sh staging repository are already integrated. But some patches are left. I guess they are busy on other CPU archs.

Quoted 7 lines

Especially, it's not a good time to apply patches which is not so serious, because version cutting work is going now.

Regards,
Shin-ichiro KAWASAKI

There is also one thing that is annoying me.

Quoted 7 lines

With the algorithm you proposed in your patch, you're not able anymore to raise the MMU_ITLB_MULTIPLE and MMU_DTLB_MULTIPLE exceptions. This makes the MMU emulation different from the real processor (even if thoses cases aren't even handled in Linux and probably others OS).

For example you can probably not map the same physical area with differents virtual page sizes.

Right. I'll consider on it for next version of the patch. Thank you!

Regards,
Shin-ichiro KAWASAKI

#7 lore raw
In reply to Shin-ichiro KAWASAKI
Quoted 9 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 5 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

Quoted 10 lines
Quoted 5 lines

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Should I try on the last qemu svn ?

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Are you working on a 32 or 64bits host system ?

I experiencing a lot of random behavior with qemu...

Quoted 1 line

From now I don't a have video anymore...

Signature · 1 line
Lionel Landwerlin <[email protected]>
#9 lore raw
In reply to Shin-ichiro KAWASAKI

Shin-ichiro,

Sorry, but I cannot apply your patch cleanly on the last qemu-svn.

Instead, I would like to try another approach. The patch you proposed to find (or not) a valid TLB entry has a complexity of O(log2(n)) (or something like that if I remember) instead here is a patch with a complexity of O(1).

This patch has the same problem concerning the multiple hit exception that is not raised anymore, but I think we can handle it with some more variables in tlb entries.

The patch apply on

qemu-svn r6215

+

git://git.kernel.org/pub/scm/virt/qemu/lethal/qemu-sh.git patches

Regards,

Signature · 23 lines
Lionel Landwerlin <[email protected]>

[PATCH] SH4: Use O(1) algorithm instead of O(n) for utlb lookup

The current algorithm to know whether a virtual address belongs to the
TLB cache is to parse all the TLB one by one. The complexity of this
algorithm is O(n).

The following patch implements a TLB entry cache, by keeping for each
virtual page number the associated TLB entry. As usual the complexity
gain is balanced by an overall memory comsumption that is about 5Mb.

This patches also introduce a behavior divergence with the real SH4
processor, because no more 'multiple hit exception' is raised, when 2
TLB map the same physical area. This will be fixed in another
iteration of this patch.

Signed-off-by: Lionel Landwerlin <[email protected]>
---
 target-sh4/cpu.h       |    8 +++
 target-sh4/helper.c    |  156 ++++++++++++++++++++++++++++++++++--------------
 target-sh4/translate.c |    9 ++-
 3 files changed, 127 insertions(+), 46 deletions(-)
target-sh4/cpu.h +8 -0
index 4a9ddea..2b17a93 100644
@@ -153,6 +153,14 @@ typedef struct CPUSH4State {
     int intr_at_halt;		/* SR_BL ignored during sleep */
     store_request_t *store_requests;
     store_request_t **store_request_tail;
+
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 2^22 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 2^20 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 2^16 => 64 Kb */
+    uint8_t utlbs_1m[4096]; /* 2^12 => 4 Kb */
+#endif
 } CPUSH4State;
 
 CPUSH4State *cpu_sh4_init(const char *cpu_model);
target-sh4/helper.c +112 -44

251 lines not on this page · raw message

target-sh4/translate.c +7 -2
index df81052..eab21a6 100644
@@ -202,6 +202,11 @@ static void cpu_sh4_reset(CPUSH4State * env)
     set_float_rounding_mode(float_round_to_zero, &env->fp_status);
 #endif
     env->mmucr = 0;
+
+    memset (env->utlbs_1k, 0, sizeof (env->utlbs_1k));
+    memset (env->utlbs_4k, 0, sizeof (env->utlbs_4k));
+    memset (env->utlbs_64k, 0, sizeof (env->utlbs_64k));
+    memset (env->utlbs_1m, 0, sizeof (env->utlbs_1m));
 }
 
 typedef struct {
@@ -510,7 +515,7 @@ static void _decode_opc(DisasContext * ctx)
        to this list.  When we see an instruction that is neither movca.l
        nor ocbi, we perform the stores recorded in this list.  When we see
        ocbi, we check if the stores list has the address being invalidated.
-       If so, we remove the address from the list.  
+       If so, we remove the address from the list.
 
        To optimize, we only try to flush stores when we're at the start of
        TB, or if we already saw movca.l in this TB and did not flush stores
@@ -1565,7 +1570,7 @@ static void _decode_opc(DisasContext * ctx)
 	}
 	return;
     case 0x00c3:		/* movca.l R0,@Rm */
-	gen_helper_movcal (REG(B11_8), REG(0));	
+	gen_helper_movcal (REG(B11_8), REG(0));
 	ctx->has_movcal = 1;
 	return;
     case 0x40a9:
Signature · 1 line
1.5.6.5
#10 lore raw

You might want this patch too

Signature · 8 lines
Lionel Landwerlin <[email protected]>

[PATCH] SH4: Fixed last UTLB unused and URB/URC management

Signed-off-by: Lionel Landwerlin <[email protected]>
---
 target-sh4/helper.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
target-sh4/helper.c +0 -0
index cd0f392..54a3f1f 100644
@@ -297,7 +297,7 @@ static int same_tlb_entry_exists(const tlb_t *

haystack, uint8_t nbtlb,

     return 0;

}

-static void increment_urc(CPUState * env)
+static inline void increment_urc(CPUState * env)

 {
     uint8_t urb, urc;

@@ -305,7 +305,7 @@ static void increment_urc(CPUState * env)

     urb = ((env->mmucr) >> 18) & 0x3f;
     urc = ((env->mmucr) >> 10) & 0x3f;
     urc++;
-    if (urc == urb || urc == UTLB_SIZE - 1)
+    if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
 	urc = 0;
     env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);

}

Signature · 1 line
1.5.6.5
#8 lore raw
In reply to Lionel Landwerlin
Quoted 15 lines · Lionel Landwerlin
Quoted 14 lines · Shin-ichiro KAWASAKI

Hi, Lionel.
Thank you for the response!

Quoted 10 lines · Lionel Landwerlin

Le vendredi 27 février 2009 à 01:28 +0900, Shin-ichiro KAWASAKI a écrit :

Hi Shin-ichiro,

I'm interested by your patch, but it does not apply on top of the r6215 (qemu svn) + qemu-sh patches
( http://git.kernel.org/?p=virt/qemu/lethal/qemu-sh.git;a=summary)

Should I try on the last qemu svn ?

Quoted 8 lines
Quoted 4 lines

Yes. When I send patch to qemu-devel ml, I make diff against qemu main repository. Could you try on it?

Are you working on a 32 or 64bits host system ?

I experiencing a lot of random behavior with qemu...

My host system is 32 bit. Core 2 Duo and VMware.
I often see sometimes user process fails with segmentation fault. I know nothing about sh4 system emulation on 64 bit host.

Quoted 1 line
Quoted 1 line

From now I don't a have video anymore...

#11 lore raw
In reply to Lionel Landwerlin
Quoted 8 lines · Lionel Landwerlin

Shin-ichiro,

Sorry, but I cannot apply your patch cleanly on the last qemu-svn.

Instead, I would like to try another approach. The patch you proposed to find (or not) a valid TLB entry has a complexity of O(log2(n)) (or something like that if I remember) instead here is a patch with a complexity of O(1).

Good work. I evaluated your patch on my environment, measuring compile time for empty main() with gcc.

  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Your patch has a nice score!

Now I've done the work to increase number of utlb entries from 64 to 256, and found that the score get arround 2.4 seconds.
I'm trying to increase it to 4096. Your O(1) search will be more important as the entry number increase.

target-sh4/cpu.h typedef struct CPUSH4State { · line 164

Quoted 8 lines
target-sh4/cpu.h +7 -0
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 222 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 220 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 216 => 64 Kb */
+    uint8_t utlbs_1m[4096]; /* 212 => 4 Kb */
+#endif
 } CPUSH4State;

Isn't it too gorgeous?
How about allocating them on demand?
I guess sh-linux uses only utlbs_4k[], in general. If so, 4 Mb utlbs_1k[] is waste.

Regards,
Shin-ichiro KAWASAKI

#12 lore raw
Quoted 13 lines · Shin-ichiro KAWASAKI
Quoted 8 lines · Lionel Landwerlin

Shin-ichiro,

Sorry, but I cannot apply your patch cleanly on the last qemu-svn.

Instead, I would like to try another approach. The patch you proposed to find (or not) a valid TLB entry has a complexity of O(log2(n)) (or something like that if I remember) instead here is a patch with a complexity of O(1).

Good work. I evaluated your patch on my environment, measuring compile time for empty main() with gcc.

Quoted 6 lines
  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Your patch has a nice score!

Great :) But we're still far from arm :(

Quoted 6 lines

Now I've done the work to increase number of utlb entries from 64 to 256, and found that the score get arround 2.4 seconds.
I'm trying to increase it to 4096. Your O(1) search will be more important as the entry number increase.

What do you mean by more important ?

Is the arm emulation increasing the number of TLB ?

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 222 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 220 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 216 => 64 Kb */
Quoted 9 lines

target-sh4/cpu.h typedef struct CPUSH4State { · line 164

Quoted 3 lines
target-sh4/cpu.h +2 -0
+    uint8_t utlbs_1m[4096]; /* 212 => 4 Kb */
+#endif
 } CPUSH4State;

Isn't it too gorgeous?
How about allocating them on demand?
I guess sh-linux uses only utlbs_4k[], in general. If so, 4 Mb utlbs_1k[] is waste.

sh-linux can also use huge pages of 64k and 1M.

I think it is important to keep the emulation as close as possible from the real the cpu capabilities.

Regards,

Signature · 1 line
Lionel Landwerlin <[email protected]>
#13 lore raw
Quoted 1 line · Lionel Landwerlin

Le mercredi 04 mars 2009 à 00:46 +0900, Shin-ichiro KAWASAKI a écrit :

[...]

Quoted 8 lines
Quoted 6 lines
  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Your patch has a nice score!

Great :) But we're still far from arm :(

It would be interesting if you could run oprofile of both platforms (and even better if that was with call-graph output).

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 222 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 220 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 216 => 64 Kb */
Quoted 9 lines

target-sh4/cpu.h typedef struct CPUSH4State { · line 164

Quoted 3 lines
target-sh4/cpu.h +2 -0
+    uint8_t utlbs_1m[4096]; /* 212 => 4 Kb */
+#endif
 } CPUSH4State;

Isn't it too gorgeous?
How about allocating them on demand?
I guess sh-linux uses only utlbs_4k[], in general. If so, 4 Mb utlbs_1k[] is waste.

Quoted 4 lines

sh-linux can also use huge pages of 64k and 1M.

I think it is important to keep the emulation as close as possible from the real the cpu capabilities.

I think Shin-ichiro was rather pointing a the number of 1k pages, which are probably not used. OTOH I agree being close to CPU capabilities is important.

Laurent

#14 lore raw
Quoted 31 lines · Laurent Desnogues
Quoted 1 line · Lionel Landwerlin

Le mercredi 04 mars 2009 à 00:46 +0900, Shin-ichiro KAWASAKI a écrit :

[...]

Quoted 8 lines
Quoted 6 lines
  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Your patch has a nice score!

Great :) But we're still far from arm :(

It would be interesting if you could run oprofile of both platforms (and even better if that was with call-graph output).

Quoted 15 lines
Quoted 14 lines

target-sh4/cpu.h typedef struct CPUSH4State { · line 164

Quoted 8 lines
target-sh4/cpu.h +7 -0
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 222 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 220 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 216 => 64 Kb */
+    uint8_t utlbs_1m[4096]; /* 212 => 4 Kb */
+#endif
 } CPUSH4State;

Isn't it too gorgeous?
How about allocating them on demand?
I guess sh-linux uses only utlbs_4k[], in general. If so, 4 Mb utlbs_1k[] is waste.

Quoted 8 lines
Quoted 4 lines

sh-linux can also use huge pages of 64k and 1M.

I think it is important to keep the emulation as close as possible from the real the cpu capabilities.

I think Shin-ichiro was rather pointing a the number of 1k pages, which are probably not used. OTOH I agree being close to CPU capabilities is important.

The problem is, when a virtual address space must be resolved, the cpu emulator is not able to know what page size has been (or not) used to map the address. Thus we must handle the worst case...

And after all, 5Mb isn't so much, considering the fact that the physical guest memory, in system emulation, can be mapped anywhere in the host virtual address space, contrary to the user emulation that have harder contraints...

By the way, does someone know why there is some kind of "tlb management code" in exec.c ??

Does the SH4 architecture have special features that can't be handled in a generic code ? Or are we just rewriting some code that is already there ... ?

Signature · 1 line
Lionel Landwerlin <[email protected]>
#18 lore raw
In reply to Shin-ichiro KAWASAKI
Quoted 18 lines · Shin-ichiro KAWASAKI
Quoted 8 lines · Lionel Landwerlin

Shin-ichiro,

Sorry, but I cannot apply your patch cleanly on the last qemu-svn.

Instead, I would like to try another approach. The patch you proposed to find (or not) a valid TLB entry has a complexity of O(log2(n)) (or something like that if I remember) instead here is a patch with a complexity of O(1).

Good work. I evaluated your patch on my environment, measuring compile time for empty main() with gcc.

  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)
Quoted 6 lines

Your patch has a nice score!

Now I've done the work to increase number of utlb entries from 64 to 256, and found that the score get arround 2.4 seconds.
I'm trying to increase it to 4096. Your O(1) search will be more important as the entry number increase.

We should setup a git repository to improve that or at least work on the same basis.

Do you have a lot of patch on top of the last svn ?

Signature · 1 line
Lionel Landwerlin <[email protected]>
#15 lore raw
In reply to Lionel Landwerlin
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Great :) But we're still far from arm :(

By the way, does someone know why there is some kind of "tlb management code" in exec.c ??

Does the SH4 architecture have special features that can't be handled in a generic code ? Or are we just rewriting some code that is already there ... ?

I think you're missing the most important difference; SH uses a software managed TLB, whereas ARM uses a hardware managed TLB.

The main consequence of this is that we don't have to model the actual ARM TLB at all, it is never directly visible. We effectively implement an infinitely large TLB.

For SH the TLB is programmed directly, so we end up having to maintain two
TLBs: The qemu TLB and the architectural SH TLB. For correct operation pages must be removed from the qemu TLB when they are evicted/replaced in the SH TLB. The SH TLB is quite small, and flushing qemu TLB entries is quite expensive, so this results in fairly poor performance.

MIPS has a similar problem. However in that case the most common TLB operations do not directly expose the TLB state. In particular when setting a new TLB entry it is unspecified which TLB entry is replaced. At that point the OS can't know which ehtry was evicted, so we can lie, and not evict pages until the guest does something that allows it to determine the exact TLB state. In practice this is sufficient to make mips-linux workreasonably well.

I'm not sure if the same is posible for SH. It probably depends whether URC is visible to/used by the guest.

Large pages add even more complications. The qemu tlb canonly handle a single page size. In practice means that when large pages are used invalidating a single page entry requires the whole qemu tlb to be flushed. I'm pretty sure x86 getsand works mainly be chance (nothing actually ues large pages enough to notice it's broken). ARM takes the hit of a full TLB flush (linux breakss if you only flush a 1k region of a 4k entry), but single pge flushes are rare so in practice this doesn't hurt too much

Paul

#19 lore raw
In reply to Lionel Landwerlin
Quoted 25 lines · Lionel Landwerlin
Quoted 17 lines · Shin-ichiro KAWASAKI
Quoted 8 lines · Lionel Landwerlin

Shin-ichiro,

Sorry, but I cannot apply your patch cleanly on the last qemu-svn.

Instead, I would like to try another approach. The patch you proposed to find (or not) a valid TLB entry has a complexity of O(log2(n)) (or something like that if I remember) instead here is a patch with a complexity of O(1).

Good work. I evaluated your patch on my environment, measuring compile time for empty main() with gcc.

  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)
Quoted 6 lines

Your patch has a nice score!

Now I've done the work to increase number of utlb entries from 64 to 256, and found that the score get arround 2.4 seconds.
I'm trying to increase it to 4096. Your O(1) search will be more important as the entry number increase.

Quoted 4 lines

We should setup a git repository to improve that or at least work on the same basis.

Do you have a lot of patch on top of the last svn ?

I personally manages my staging repository corresponds to 'qemu-sh'.

git://github.com/kawasaki/qemu-sh.git
http://github.com/kawasaki/qemu-sh/tree/master

Patches in it can be applied to current qemu subversion trunk. I put your O(1) patch and my utlb entry increasing patch also. Will it work as the basis?

I attach the utlb entry increasing patch to this mail for reference. It should be applied with other patches in my staging repository, or patch command will fail.
It increases the number of utlb entry from 64 to 256.
URC and URB values in MMUCR can take the number from 0 to 255.

Here's the benchmark value.

  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  sh4 : 2.1 [seconds]     O(1) utlb search, and 256 utlb enries  <= 
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Getting better :) but still behind arm.

Regards,
Shin-ichiro KAWASAKI

target-sh4/cpu.h +0 -0
Index: trunk/target-sh4/cpu.h
===================================================================
@@ -88,7 +88,8 @@
    uint8_t tc;			/* timing control */

} tlb_t;

-#define UTLB_SIZE 64
+#define UTLB_BITS 8 /* real hard : 6 */
+#define UTLB_SIZE (1 << UTLB_BITS)
#define ITLB_SIZE 4

#define NB_MMU_MODES 2
@@ -228,14 +229,18 @@
#define MMUCR 0x1F000010
#define MMUCR_AT (1<<0)
#define MMUCR_SV (1<<8)
-#define MMUCR_URC_BITS (6)
#define MMUCR_URC_OFFSET (10)
-#define MMUCR_URC_SIZE (1 << MMUCR_URC_BITS)
-#define MMUCR_URC_MASK (((MMUCR_URC_SIZE) - 1) << MMUCR_URC_OFFSET) +#define MMUCR_URC_MASK ((UTLB_SIZE - 1) << MMUCR_URC_OFFSET) static inline int cpu_mmucr_urc (uint32_t mmucr)

{
    return ((mmucr & MMUCR_URC_MASK) >> MMUCR_URC_OFFSET);
}

+#define MMUCR_URB_OFFSET (18)
+#define MMUCR_URB_MASK ((UTLB_SIZE - 1) << MMUCR_URB_OFFSET) +static inline int cpu_mmucr_urb (uint32_t mmucr) +{
+ return ((mmucr & MMUCR_URB_MASK) >> MMUCR_URB_OFFSET);
+}

/* PTEH : Page Translation Entry High register */
#define PTEH_ASID_BITS (8)

target-sh4/helper.c +4 -4
Index: trunk/target-sh4/helper.c
===================================================================
@@ -346,12 +346,12 @@
    uint8_t urb, urc;
 
    /* Increment URC */
-    urb = ((env->mmucr) >> 18) & 0x3f;
-    urc = ((env->mmucr) >> 10) & 0x3f;
+    urb = cpu_mmucr_urb(env->mmucr);
+    urc = cpu_mmucr_urc(env->mmucr);
    urc++;
    if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
-	urc = 0;
-    env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
+        urc = 0;
+    env->mmucr = (env->mmucr & ~MMUCR_URC_MASK) | (urc << MMUCR_URC_OFFSET);

}

/* Find itlb entry - update itlb from utlb if necessary and asked for

#16 lore raw
In reply to Paul Brook
Quoted 11 lines · Paul Brook
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Great :) But we're still far from arm :(

By the way, does someone know why there is some kind of "tlb management code" in exec.c ??

Does the SH4 architecture have special features that can't be handled in a generic code ? Or are we just rewriting some code that is already there ... ?

I think you're missing the most important difference; SH uses a software managed TLB, whereas ARM uses a hardware managed TLB.

Quoted 7 lines

The main consequence of this is that we don't have to model the actual ARM TLB at all, it is never directly visible. We effectively implement an infinitely large TLB.

For SH the TLB is programmed directly, so we end up having to maintain two TLBs: The qemu TLB and the architectural SH TLB. For correct operation pages must be removed from the qemu TLB when they are evicted/replaced in the SH TLB. The SH TLB is quite small, and flushing qemu TLB entries is quite expensive, so this results in fairly poor performance.

MIPS has a similar problem. However in that case the most common TLB operations do not directly expose the TLB state. In particular when setting a new TLB entry it is unspecified which TLB entry is replaced. At that point the OS can't know which ehtry was evicted, so we can lie, and not evict pages until the guest does something that allows it to determine the exact TLB state. In practice this is sufficient to make mips-linux workreasonably well.

I'm not sure if the same is posible for SH. It probably depends whether URC is visible to/used by the guest.

Thank you Paul for your clear explanation. It confirms my guess, and answers my question also.

As I posted for other mail, I tried to increase the number of tlb entry from 64 to 256 and get performance improvement. This approach modifies real hardware specification including URC, but same SH-Linux kernel works fine. It implies that SH-Linux does not refer URC, and MIPS approach might be the solution for SH too.

Regards,
Shin-ichiro KAWASAKI

#17 lore raw
In reply to Lionel Landwerlin
Quoted 36 lines · Lionel Landwerlin
Quoted 27 lines · Laurent Desnogues
Quoted 1 line · Lionel Landwerlin

Le mercredi 04 mars 2009 à 00:46 +0900, Shin-ichiro KAWASAKI a écrit :

[...]

Quoted 7 lines
Quoted 6 lines
  sh4 : 5.8 [seconds]     O(n) utlb search.
  sh4 : 4.6 [seconds]     O(log2(n)) utlb search.
  sh4 : 4.1 [seconds]     O(1) utlb search by Lionel
  arm : 0.8 [seconds]     (-M versatilepb + Debian ARM)

Your patch has a nice score!

Great :) But we're still far from arm :(

It would be interesting if you could run oprofile of both platforms (and even better if that was with call-graph output).

Quoted 13 lines
Quoted 13 lines

target-sh4/cpu.h typedef struct CPUSH4State { · line 164

Quoted 8 lines
target-sh4/cpu.h +7 -0
+#if !defined(CONFIG_USER_ONLY)
+    /* vpn to utlb entry caches (too much space for user emulation) */
+    uint8_t utlbs_1k[4194304]; /* 222 => 4 Mb */
+    uint8_t utlbs_4k[1048576]; /* 220 => 1 Mb */
+    uint8_t utlbs_64k[65536]; /* 216 => 64 Kb */
+    uint8_t utlbs_1m[4096]; /* 212 => 4 Kb */
+#endif
 } CPUSH4State;

Isn't it too gorgeous?
How about allocating them on demand?
I guess sh-linux uses only utlbs_4k[], in general. If so, 4 Mb utlbs_1k[] is waste.

Quoted 7 lines
Quoted 4 lines

sh-linux can also use huge pages of 64k and 1M.

I think it is important to keep the emulation as close as possible from the real the cpu capabilities.

I think Shin-ichiro was rather pointing a the number of 1k pages, which are probably not used. OTOH I agree being close to CPU capabilities is important.

Quoted 8 lines

The problem is, when a virtual address space must be resolved, the cpu emulator is not able to know what page size has been (or not) used to map the address. Thus we must handle the worst case...

And after all, 5Mb isn't so much, considering the fact that the physical guest memory, in system emulation, can be mapped anywhere in the host virtual address space, contrary to the user emulation that have harder contraints...

IMO, it will be another patch which introduces lines like this.

if (!env->utlbs_1k)

      env->utlbs_1k = qemu_mallocz(4194304);

But footprint tuning can be left as future task.
Sorry for being too critical.

Regards,
Shin-ichiro KAWASAKI