| 1 |
/* |
| 2 |
* vmstat.c - statistics gathering code for vm.c |
| 3 |
* |
| 4 |
* Copyright (c) 2005-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
/* This file is included from vm.c */ |
| 35 |
|
| 36 |
#ifdef COUNT_INSN_FREQUENCY |
| 37 |
/* for statistics */ |
| 38 |
static u_long insn1_freq[SCM_VM_NUM_INSNS]; |
| 39 |
static u_long insn2_freq[SCM_VM_NUM_INSNS][SCM_VM_NUM_INSNS]; |
| 40 |
|
| 41 |
#define LREF_FREQ_COUNT_MAX 10 |
| 42 |
static u_long lref_freq[LREF_FREQ_COUNT_MAX][LREF_FREQ_COUNT_MAX]; |
| 43 |
static u_long lset_freq[LREF_FREQ_COUNT_MAX][LREF_FREQ_COUNT_MAX]; |
| 44 |
|
| 45 |
static ScmWord fetch_insn_counting(ScmVM *vm, ScmWord code) |
| 46 |
{ |
| 47 |
if (vm->base && vm->pc != vm->base->code) { |
| 48 |
insn2_freq[SCM_VM_INSN_CODE(code)][SCM_VM_INSN_CODE(*vm->pc)]++; |
| 49 |
} |
| 50 |
code = *vm->pc++; |
| 51 |
insn1_freq[SCM_VM_INSN_CODE(code)]++; |
| 52 |
switch (SCM_VM_INSN_CODE(code)) { |
| 53 |
case SCM_VM_LREF0: lref_freq[0][0]++; break; |
| 54 |
case SCM_VM_LREF1: lref_freq[0][1]++; break; |
| 55 |
case SCM_VM_LREF2: lref_freq[0][2]++; break; |
| 56 |
case SCM_VM_LREF3: lref_freq[0][3]++; break; |
| 57 |
case SCM_VM_LREF4: lref_freq[0][4]++; break; |
| 58 |
case SCM_VM_LREF10: lref_freq[1][0]++; break; |
| 59 |
case SCM_VM_LREF11: lref_freq[1][1]++; break; |
| 60 |
case SCM_VM_LREF12: lref_freq[1][2]++; break; |
| 61 |
case SCM_VM_LREF13: lref_freq[1][3]++; break; |
| 62 |
case SCM_VM_LREF14: lref_freq[1][4]++; break; |
| 63 |
case SCM_VM_LREF: |
| 64 |
{ |
| 65 |
int dep = SCM_VM_INSN_ARG0(code); |
| 66 |
int off = SCM_VM_INSN_ARG1(code); |
| 67 |
if (dep >= LREF_FREQ_COUNT_MAX) dep=LREF_FREQ_COUNT_MAX-1; |
| 68 |
if (off >= LREF_FREQ_COUNT_MAX) off=LREF_FREQ_COUNT_MAX-1; |
| 69 |
lref_freq[dep][off]++; |
| 70 |
break; |
| 71 |
} |
| 72 |
case SCM_VM_LSET0: lset_freq[0][0]++; break; |
| 73 |
case SCM_VM_LSET1: lset_freq[0][1]++; break; |
| 74 |
case SCM_VM_LSET2: lset_freq[0][2]++; break; |
| 75 |
case SCM_VM_LSET3: lset_freq[0][3]++; break; |
| 76 |
case SCM_VM_LSET4: lset_freq[0][4]++; break; |
| 77 |
case SCM_VM_LSET: |
| 78 |
{ |
| 79 |
int dep = SCM_VM_INSN_ARG0(code); |
| 80 |
int off = SCM_VM_INSN_ARG1(code); |
| 81 |
if (dep >= LREF_FREQ_COUNT_MAX) dep=LREF_FREQ_COUNT_MAX-1; |
| 82 |
if (off >= LREF_FREQ_COUNT_MAX) off=LREF_FREQ_COUNT_MAX-1; |
| 83 |
lset_freq[dep][off]++; |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
return code; |
| 88 |
} |
| 89 |
|
| 90 |
static void dump_insn_frequency(void *data) |
| 91 |
{ |
| 92 |
Scm_Printf(SCM_CUROUT, "(:instruction-frequencies ("); |
| 93 |
for (int i=0; i<SCM_VM_NUM_INSNS; i++) { |
| 94 |
Scm_Printf(SCM_CUROUT, "(%s %d", Scm_VMInsnName(i), insn1_freq[i]); |
| 95 |
for (int j=0; j<SCM_VM_NUM_INSNS; j++) { |
| 96 |
Scm_Printf(SCM_CUROUT, " %d", insn2_freq[i][j]); |
| 97 |
} |
| 98 |
Scm_Printf(SCM_CUROUT, ")\n"); |
| 99 |
} |
| 100 |
Scm_Printf(SCM_CUROUT, ")\n :lref-frequencies ("); |
| 101 |
for (int i=0; i<LREF_FREQ_COUNT_MAX; i++) { |
| 102 |
Scm_Printf(SCM_CUROUT, "("); |
| 103 |
for (int j=0; j<LREF_FREQ_COUNT_MAX; j++) { |
| 104 |
Scm_Printf(SCM_CUROUT, "%d ", lref_freq[i][j]); |
| 105 |
} |
| 106 |
Scm_Printf(SCM_CUROUT, ")\n"); |
| 107 |
} |
| 108 |
Scm_Printf(SCM_CUROUT, ")\n :lset-frequencies ("); |
| 109 |
for (int i=0; i<LREF_FREQ_COUNT_MAX; i++) { |
| 110 |
Scm_Printf(SCM_CUROUT, "("); |
| 111 |
for (int j=0; j<LREF_FREQ_COUNT_MAX; j++) { |
| 112 |
Scm_Printf(SCM_CUROUT, "%d ", lset_freq[i][j]); |
| 113 |
} |
| 114 |
Scm_Printf(SCM_CUROUT, ")\n"); |
| 115 |
} |
| 116 |
Scm_Printf(SCM_CUROUT, ")\n"); |
| 117 |
Scm_Printf(SCM_CUROUT, ")\n"); |
| 118 |
} |
| 119 |
|
| 120 |
#endif /*COUNT_INSN_FREQUENCY*/ |