Annotation of hatari/src/debug/profiledsp.c, revision 1.1.1.4

1.1       root        1: /*
                      2:  * Hatari - profiledsp.c
                      3:  * 
1.1.1.3   root        4:  * Copyright (C) 2010-2015 by Eero Tamminen
1.1       root        5:  *
                      6:  * This file is distributed under the GNU General Public License, version 2
                      7:  * or at your option any later version. Read the file gpl.txt for details.
                      8:  *
                      9:  * profiledsp.c - functions for profiling DSP and showing the results.
                     10:  */
                     11: const char Profiledsp_fileid[] = "Hatari profiledsp.c : " __DATE__ " " __TIME__;
                     12: 
                     13: #include <stdio.h>
                     14: #include <inttypes.h>
1.1.1.4 ! root       15: #include <limits.h>
1.1       root       16: #include <assert.h>
                     17: #include "main.h"
                     18: #include "configuration.h"
                     19: #include "clocks_timings.h"
                     20: #include "dsp.h"
1.1.1.4 ! root       21: #include "symbols.h"
1.1       root       22: #include "profile.h"
                     23: #include "profile_priv.h"
1.1.1.4 ! root       24: #include "debug_priv.h"
1.1.1.2   root       25: /* for VBL info */
                     26: #include "screen.h"
                     27: #include "video.h"
1.1       root       28: 
                     29: static callinfo_t dsp_callinfo;
                     30: 
                     31: #define DSP_PROFILE_ARR_SIZE 0x10000
                     32: #define MAX_DSP_PROFILE_VALUE 0xFFFFFFFFFFFFFFFFLL
                     33: 
                     34: typedef struct {
                     35:        Uint64 count;           /* how many times this address is used */
                     36:        Uint64 cycles;          /* how many DSP cycles was taken at this address */
                     37:        Uint16 min_cycle;
                     38:        Uint16 max_cycle;
                     39: } dsp_profile_item_t;
                     40: 
                     41: static struct {
                     42:        dsp_profile_item_t *data; /* profile data */
                     43:        profile_area_t ram;   /* statistics for whole memory */
                     44:        Uint16 *sort_arr;     /* data indexes used for sorting */
                     45:        Uint16 prev_pc;       /* previous PC for which the cycles are for */
1.1.1.2   root       46:        Uint16 loop_start;    /* address of last loop start */
                     47:        Uint16 loop_end;      /* address of last loop end */
                     48:        Uint32 loop_count;    /* how many times it was looped */
1.1       root       49:        Uint32 disasm_addr;   /* 'dspaddresses' command start address */
                     50:        bool processed;       /* true when data is already processed */
                     51:        bool enabled;         /* true when profiling enabled */
                     52: } dsp_profile;
                     53: 
                     54: 
                     55: /* ------------------ DSP profile results ----------------- */
                     56: 
                     57: /**
                     58:  * Get DSP cycles, count and count percentage for given address.
                     59:  * Return true if data was available and non-zero, false otherwise.
                     60:  */
                     61: bool Profile_DspAddressData(Uint16 addr, float *percentage, Uint64 *count, Uint64 *cycles, Uint16 *cycle_diff)
                     62: {
                     63:        dsp_profile_item_t *item;
                     64:        if (!dsp_profile.data) {
                     65:                return false;
                     66:        }
                     67:        item = dsp_profile.data + addr;
                     68: 
                     69:        *cycles = item->cycles;
                     70:        *count = item->count;
                     71:        if (item->max_cycle) {
                     72:                *cycle_diff = item->max_cycle - item->min_cycle;
                     73:        } else {
                     74:                *cycle_diff = 0;
                     75:        }
                     76:        if (dsp_profile.ram.counters.count) {
                     77:                *percentage = 100.0*(*count)/dsp_profile.ram.counters.count;
                     78:        } else {
                     79:                *percentage = 0.0;
                     80:        }
                     81:        return (*count > 0);
                     82: }
                     83: 
                     84: /**
                     85:  * show DSP specific profile statistics.
                     86:  */
                     87: void Profile_DspShowStats(void)
                     88: {
                     89:        profile_area_t *area = &dsp_profile.ram;
                     90:        fprintf(stderr, "DSP profile statistics (0x0-0xFFFF):\n");
                     91:        if (!area->active) {
                     92:                fprintf(stderr, "- no activity\n");
                     93:                return;
                     94:        }
                     95:        fprintf(stderr, "- active address range:\n  0x%04x-0x%04x\n",
                     96:                area->lowest, area->highest);
                     97:        fprintf(stderr, "- active instruction addresses:\n  %d\n",
                     98:                area->active);
                     99:        fprintf(stderr, "- executed instructions:\n  %"PRIu64"\n",
                    100:                area->counters.count);
                    101:        /* indicates either instruction(s) that address different memory areas
                    102:         * (they can have different access costs), or more significantly,
                    103:         * DSP code that has changed during profiling.
                    104:         */
                    105:        fprintf(stderr, "- sum of per instruction cycle changes\n"
                    106:                "  (can indicate code change during profiling):\n  %"PRIu64"\n",
1.1.1.3   root      107:                area->counters.cycles_diffs);
1.1       root      108: 
                    109:        fprintf(stderr, "- used cycles:\n  %"PRIu64"\n",
                    110:                area->counters.cycles);
                    111:        if (area->overflow) {
                    112:                fprintf(stderr, "  *** COUNTERS OVERFLOW! ***\n");
                    113:        }
                    114:        fprintf(stderr, "\n= %.5fs\n", (double)(area->counters.cycles) / MachineClocks.DSP_Freq);
                    115: }
                    116: 
                    117: /**
                    118:  * Show DSP instructions which execution was profiled, in the address order,
                    119:  * starting from the given address.  Return next disassembly address.
                    120:  */
1.1.1.4 ! root      121: Uint16 Profile_DspShowAddresses(Uint32 addr, Uint32 upper, FILE *out, paging_t use_paging)
1.1       root      122: {
1.1.1.4 ! root      123:        int show, shown, addrs, active;
1.1       root      124:        dsp_profile_item_t *data;
                    125:        Uint16 nextpc;
                    126:        Uint32 end;
                    127:        const char *symbol;
                    128: 
                    129:        data = dsp_profile.data;
                    130:        if (!data) {
                    131:                fprintf(stderr, "ERROR: no DSP profiling data available!\n");
                    132:                return 0;
                    133:        }
                    134: 
                    135:        end = DSP_PROFILE_ARR_SIZE;
                    136:        active = dsp_profile.ram.active;
                    137:        if (upper) {
                    138:                if (upper < end) {
                    139:                        end = upper;
                    140:                }
                    141:                show = active;
                    142:        } else {
1.1.1.4 ! root      143:                show = DebugUI_GetPageLines(ConfigureParams.Debugger.nDisasmLines, 0);
1.1       root      144:                if (!show || show > active) {
                    145:                        show = active;
                    146:                }
                    147:        }
1.1.1.4 ! root      148:        if (use_paging == PAGING_DISABLED) {
        !           149:                show = INT_MAX;
        !           150:        }
1.1       root      151: 
                    152:        fputs("# disassembly with profile data: <instructions percentage>% (<sum of instructions>, <sum of cycles>, <max cycle difference>)\n", out);
1.1.1.4 ! root      153:        shown = 2; /* first and last printf */
1.1       root      154: 
1.1.1.4 ! root      155:        addrs = nextpc = 0;
        !           156:        for (; shown < show && addr < end; addr++) {
1.1       root      157:                if (!data[addr].count) {
                    158:                        continue;
                    159:                }
                    160:                if (addr != nextpc && nextpc) {
                    161:                        fputs("[...]\n", out);
1.1.1.4 ! root      162:                        shown++;
1.1       root      163:                }
1.1.1.4 ! root      164:                symbol = Symbols_GetByDspAddress(addr, SYMTYPE_TEXT);
1.1       root      165:                if (symbol) {
                    166:                        fprintf(out, "%s:\n", symbol);
1.1.1.4 ! root      167:                        shown++;
1.1       root      168:                }
                    169:                nextpc = DSP_DisasmAddress(out, addr, addr);
1.1.1.4 ! root      170:                addrs++;
1.1       root      171:                shown++;
                    172:        }
1.1.1.4 ! root      173:        printf("Disassembled %d (of active %d) DSP addresses.\n", addrs, active);
1.1       root      174:        return nextpc;
                    175: }
                    176: 
                    177: /**
                    178:  * compare function for qsort() to sort DSP profile data by descdending
                    179:  * address cycles counts.
                    180:  */
                    181: static int cmp_dsp_cycles(const void *p1, const void *p2)
                    182: {
                    183:        Uint64 count1 = dsp_profile.data[*(const Uint16*)p1].cycles;
                    184:        Uint64 count2 = dsp_profile.data[*(const Uint16*)p2].cycles;
                    185:        if (count1 > count2) {
                    186:                return -1;
                    187:        }
                    188:        if (count1 < count2) {
                    189:                return 1;
                    190:        }
                    191:        return 0;
                    192: }
                    193: 
                    194: /**
                    195:  * Sort DSP profile data addresses by cycle counts and show the results.
                    196:  */
                    197: void Profile_DspShowCycles(int show)
                    198: {
                    199:        int active;
                    200:        Uint16 *sort_arr, *end, addr;
                    201:        dsp_profile_item_t *data = dsp_profile.data;
                    202:        float percentage;
                    203:        Uint64 count;
                    204: 
                    205:        if (!data) {
                    206:                fprintf(stderr, "ERROR: no DSP profiling data available!\n");
                    207:                return;
                    208:        }
                    209: 
                    210:        active = dsp_profile.ram.active;
                    211:        sort_arr = dsp_profile.sort_arr;
                    212:        qsort(sort_arr, active, sizeof(*sort_arr), cmp_dsp_cycles);
                    213: 
                    214:        printf("addr:\tcycles:\n");
                    215:        show = (show < active ? show : active);
                    216:        for (end = sort_arr + show; sort_arr < end; sort_arr++) {
                    217:                addr = *sort_arr;
                    218:                count = data[addr].cycles;
                    219:                percentage = 100.0*count/dsp_profile.ram.counters.cycles;
                    220:                printf("0x%04x\t%5.2f%%\t%"PRIu64"%s\n", addr, percentage, count,
                    221:                       count == MAX_DSP_PROFILE_VALUE ? " (OVERFLOW)" : "");
                    222:        }
                    223:        printf("%d DSP addresses listed.\n", show);
                    224: }
                    225: 
                    226: 
                    227: /**
                    228:  * compare function for qsort() to sort DSP profile data by descdending
                    229:  * address access counts.
                    230:  */
                    231: static int cmp_dsp_count(const void *p1, const void *p2)
                    232: {
                    233:        Uint64 count1 = dsp_profile.data[*(const Uint16*)p1].count;
                    234:        Uint64 count2 = dsp_profile.data[*(const Uint16*)p2].count;
                    235:        if (count1 > count2) {
                    236:                return -1;
                    237:        }
                    238:        if (count1 < count2) {
                    239:                return 1;
                    240:        }
                    241:        return 0;
                    242: }
                    243: 
                    244: /**
                    245:  * Sort DSP profile data addresses by call counts and show the results.
                    246:  * If symbols are requested and symbols are loaded, show (only) addresses
                    247:  * matching a symbol.
                    248:  */
                    249: void Profile_DspShowCounts(int show, bool only_symbols)
                    250: {
                    251:        dsp_profile_item_t *data = dsp_profile.data;
                    252:        int symbols, matched, active;
                    253:        Uint16 *sort_arr, *end, addr;
                    254:        const char *name;
                    255:        float percentage;
                    256:        Uint64 count;
                    257: 
                    258:        if (!data) {
                    259:                fprintf(stderr, "ERROR: no DSP profiling data available!\n");
                    260:                return;
                    261:        }
                    262:        active = dsp_profile.ram.active;
                    263:        show = (show < active ? show : active);
                    264: 
                    265:        sort_arr = dsp_profile.sort_arr;
                    266:        qsort(sort_arr, active, sizeof(*sort_arr), cmp_dsp_count);
                    267: 
                    268:        if (!only_symbols) {
                    269:                printf("addr:\tcount:\n");
                    270:                for (end = sort_arr + show; sort_arr < end; sort_arr++) {
                    271:                        addr = *sort_arr;
                    272:                        count = data[addr].count;
                    273:                        percentage = 100.0*count/dsp_profile.ram.counters.count;
                    274:                        printf("0x%04x\t%5.2f%%\t%"PRIu64"%s\n",
                    275:                               addr, percentage, count,
                    276:                               count == MAX_DSP_PROFILE_VALUE ? " (OVERFLOW)" : "");
                    277:                }
                    278:                printf("%d DSP addresses listed.\n", show);
                    279:                return;
                    280:        }
                    281: 
1.1.1.4 ! root      282:        symbols = Symbols_DspCodeCount();
1.1       root      283:        if (!symbols) {
                    284:                fprintf(stderr, "ERROR: no DSP symbols loaded!\n");
                    285:                return;
                    286:        }
                    287:        matched = 0;    
                    288: 
                    289:        printf("addr:\tcount:\t\tsymbol:\n");
                    290:        for (end = sort_arr + active; sort_arr < end; sort_arr++) {
                    291: 
                    292:                addr = *sort_arr;
1.1.1.4 ! root      293:                name = Symbols_GetByDspAddress(addr, SYMTYPE_TEXT);
1.1       root      294:                if (!name) {
                    295:                        continue;
                    296:                }
                    297:                count = data[addr].count;
                    298:                percentage = 100.0*count/dsp_profile.ram.counters.count;
                    299:                printf("0x%04x\t%.2f%%\t%"PRIu64"\t%s%s\n",
                    300:                       addr, percentage, count, name,
                    301:                       count == MAX_DSP_PROFILE_VALUE ? " (OVERFLOW)" : "");
                    302: 
                    303:                matched++;
                    304:                if (matched >= show || matched >= symbols) {
                    305:                        break;
                    306:                }
                    307:        }
                    308:        printf("%d DSP symbols listed.\n", matched);
                    309: }
                    310: 
                    311: 
                    312: static const char * addr2name(Uint32 addr, Uint64 *total)
                    313: {
                    314:        *total = dsp_profile.data[addr].count;
1.1.1.4 ! root      315:        return Symbols_GetByDspAddress(addr, SYMTYPE_TEXT);
1.1       root      316: }
                    317: 
                    318: /**
                    319:  * Output DSP callers info to given file.
                    320:  */
                    321: void Profile_DspShowCallers(FILE *fp)
                    322: {
                    323:        Profile_ShowCallers(fp, dsp_callinfo.sites, dsp_callinfo.site, addr2name);
                    324: }
                    325: 
                    326: /**
                    327:  * Save DSP profile information to given file.
                    328:  */
                    329: void Profile_DspSave(FILE *out)
                    330: {
                    331:        /* Comma separated descriptions for the profile disassembly data fields.
                    332:         * Instructions and cycles need to be first two fields!
                    333:         */
                    334:        fputs("Field names:\tExecuted instructions, Used cycles, Largest cycle differences (= code changes during profiling)\n", out);
                    335:        /* (Python) pegexp that matches address and all describled fields from disassembly:
                    336:         * <space>:<address> <opcodes> (<instr cycles>) <instr> <count>% (<count>, <cycles>)
                    337:         * p:0202  0aa980 000200  (07 cyc)  jclr #0,x:$ffe9,p:$0200  0.00% (6, 42)
                    338:         */
                    339:        fputs("Field regexp:\t^p:([0-9a-f]+) .*% \\((.*)\\)$\n", out);
1.1.1.4 ! root      340:        Profile_DspShowAddresses(0, DSP_PROFILE_ARR_SIZE, out, PAGING_DISABLED);
1.1       root      341:        Profile_DspShowCallers(out);
                    342: }
                    343: 
                    344: /* ------------------ DSP profile control ----------------- */
                    345: 
                    346: /**
                    347:  * Initialize DSP profiling when necessary.  Return true if profiling.
                    348:  */
                    349: bool Profile_DspStart(void)
                    350: {
                    351:        dsp_profile_item_t *item;
                    352:        int i;
                    353: 
                    354:        Profile_FreeCallinfo(&(dsp_callinfo));
                    355:        if (dsp_profile.sort_arr) {
                    356:                /* remove previous results */
                    357:                free(dsp_profile.sort_arr);
                    358:                free(dsp_profile.data);
                    359:                dsp_profile.sort_arr = NULL;
                    360:                dsp_profile.data = NULL;
                    361:                printf("Freed previous DSP profile buffers.\n");
                    362:        }
                    363:        if (!dsp_profile.enabled) {
                    364:                return false;
                    365:        }
                    366:        /* zero everything */
                    367:        memset(&dsp_profile, 0, sizeof(dsp_profile));
                    368: 
                    369:        dsp_profile.data = calloc(DSP_PROFILE_ARR_SIZE, sizeof(*dsp_profile.data));
                    370:        if (!dsp_profile.data) {
                    371:                perror("ERROR, new DSP profile buffer alloc failed");
                    372:                return false;
                    373:        }
                    374:        printf("Allocated DSP profile buffer (%d KB).\n",
                    375:               (int)sizeof(*dsp_profile.data)*DSP_PROFILE_ARR_SIZE/1024);
                    376: 
1.1.1.4 ! root      377:        Profile_AllocCallinfo(&(dsp_callinfo), Symbols_DspCodeCount(), "DSP");
1.1       root      378: 
                    379:        item = dsp_profile.data;
                    380:        for (i = 0; i < DSP_PROFILE_ARR_SIZE; i++, item++) {
                    381:                item->min_cycle = 0xFFFF;
                    382:        }
                    383:        dsp_profile.prev_pc = DSP_GetPC();
                    384: 
1.1.1.2   root      385:        dsp_profile.loop_start = 0xFFFF;
                    386:        dsp_profile.loop_end = 0xFFFF;
                    387:        dsp_profile.loop_count = 0;
                    388:        Profile_LoopReset();
                    389: 
1.1       root      390:        dsp_profile.disasm_addr = 0;
                    391:        dsp_profile.processed = false;
                    392:        dsp_profile.enabled = true;
                    393:        return dsp_profile.enabled;
                    394: }
                    395: 
                    396: /* return true if pc is next instruction for previous pc */
                    397: static bool is_prev_instr(Uint16 prev_pc, Uint16 pc)
                    398: {
                    399:        /* just moved to next instruction (1-2 words)? */
                    400:        if (prev_pc < pc && (pc - prev_pc) <= 4) {
                    401:                return true;
                    402:        }
                    403:        return false;
                    404: }
                    405: 
                    406: /* return branch type based on caller instruction type */
                    407: static calltype_t dsp_opcode_type(Uint16 prev_pc, Uint16 pc)
                    408: {
                    409:        const char *dummy;
                    410:        Uint32 opcode;
                    411: 
                    412:        /* 24-bit instruction opcode */
                    413:        opcode = DSP_ReadMemory(prev_pc, 'P', &dummy) & 0xFFFFFF;
                    414: 
                    415:        /* subroutine returns */
                    416:        if (opcode == 0xC) {    /* (just) RTS */
                    417:                return CALL_SUBRETURN;
                    418:        }
                    419:        /* unconditional subroutine calls */
                    420:        if ((opcode & 0xFFF000) == 0xD0000 ||   /* JSR   00001101 0000aaaa aaaaaaaa */
                    421:            (opcode & 0xFFC0FF) == 0xBC080) {   /* JSR   00001011 11MMMRRR 10000000 */
                    422:                return CALL_SUBROUTINE;
                    423:        }
                    424:        /* conditional subroutine calls */
                    425:        if ((opcode & 0xFF0000) == 0xF0000 ||   /* JSCC  00001111 CCCCaaaa aaaaaaaa */
                    426:            (opcode & 0xFFC0F0) == 0xBC0A0 ||   /* JSCC  00001011 11MMMRRR 1010CCCC */
                    427:            (opcode & 0xFFC0A0) == 0xB4080 ||   /* JSCLR 00001011 01MMMRRR 1S0bbbbb */
                    428:            (opcode & 0xFFC0A0) == 0xB0080 ||   /* JSCLR 00001011 00aaaaaa 1S0bbbbb */
                    429:            (opcode & 0xFFC0A0) == 0xB8080 ||   /* JSCLR 00001011 10pppppp 1S0bbbbb */
                    430:            (opcode & 0xFFC0E0) == 0xBC000 ||   /* JSCLR 00001011 11DDDDDD 000bbbbb */
                    431:            (opcode & 0xFFC0A0) == 0xB40A0 ||   /* JSSET 00001011 01MMMRRR 1S1bbbbb */
                    432:            (opcode & 0xFFC0A0) == 0xB00A0 ||   /* JSSET 00001011 00aaaaaa 1S1bbbbb */
                    433:            (opcode & 0xFFC0A0) == 0xB80A0 ||   /* JSSET 00001011 10pppppp 1S1bbbbb */
                    434:            (opcode & 0xFFC0E0) == 0xBC020) {   /* JSSET 00001011 11DDDDDD 001bbbbb */
                    435:                /* hopefully fairly safe heuristic:
                    436:                 * if previously executed instruction
                    437:                 * was one before current one, no
                    438:                 * subroutine call was made to next
                    439:                 * instruction, the condition just
                    440:                 * wasn't met.
                    441:                 */
                    442:                if (is_prev_instr(prev_pc, pc)) {
                    443:                        return CALL_NEXT;
                    444:                }
                    445:                return CALL_SUBROUTINE;
                    446:        }
                    447:        /* exception handler returns */
                    448:        if (opcode == 0x4) {    /* (just) RTI */
                    449:                return CALL_EXCRETURN;
                    450:        }
                    451: 
                    452:        /* Besides CALL_UNKNOWN, rest isn't used by subroutine call
                    453:         * cost collection.  However, it's useful info when debugging
                    454:         * code or reading full callgraphs (because optimized code uses
                    455:         * also jumps/branches for subroutine calls).
                    456:         */
                    457: 
                    458:        /* TODO: exception invocation.
                    459:         * Could be detected by PC going through low interrupt vector adresses,
                    460:         * but fast-calls using JSR/RTS would need separate handling.
                    461:         */
                    462:        if (0) {        /* TODO */
                    463:                return CALL_EXCEPTION;
                    464:        }
                    465:        /* branches */
                    466:        if ((opcode & 0xFFF000) == 0xC0000 ||   /* JMP  00001100 0000aaaa aaaaaaaa */
                    467:            (opcode & 0xFFC0FF) == 0xAC080 ||   /* JMP  00001010 11MMMRRR 10000000 */
                    468:            (opcode & 0xFF0000) == 0xE0000 ||   /* JCC  00001110 CCCCaaaa aaaaaaaa */
                    469:            (opcode & 0xFFC0F0) == 0xAC0A0 ||   /* JCC  00001010 11MMMRRR 1010CCCC */
                    470:            (opcode & 0xFFC0A0) == 0xA8080 ||   /* JCLR 00001010 10pppppp 1S0bbbbb */
                    471:            (opcode & 0xFFC0A0) == 0xA4080 ||   /* JCLR 00001010 01MMMRRR 1S0bbbbb */
                    472:            (opcode & 0xFFC0A0) == 0xA0080 ||   /* JCLR 00001010 00aaaaaa 1S0bbbbb */
                    473:            (opcode & 0xFFC0E0) == 0xAC000 ||   /* JCLR 00001010 11dddddd 000bbbbb */
                    474:            (opcode & 0xFFC0A0) == 0xA80A0 ||   /* JSET 00001010 10pppppp 1S1bbbbb */
                    475:            (opcode & 0xFFC0A0) == 0xA40A0 ||   /* JSET 00001010 01MMMRRR 1S1bbbbb */
                    476:            (opcode & 0xFFC0A0) == 0xA00A0 ||   /* JSET 00001010 00aaaaaa 1S1bbbbb */
                    477:            (opcode & 0xFFC0E0) == 0xAC020 ||   /* JSET 00001010 11dddddd 001bbbbb */
                    478:            (opcode & 0xFF00F0) == 0x600A0 ||   /* REP  00000110 iiiiiiii 1010hhhh */
                    479:            (opcode & 0xFFC0FF) == 0x6C020 ||   /* REP  00000110 11dddddd 00100000 */
                    480:            (opcode & 0xFFC0BF) == 0x64020 ||   /* REP  00000110 01MMMRRR 0s100000 */
                    481:            (opcode & 0xFFC0BF) == 0x60020 ||   /* REP  00000110 00aaaaaa 0s100000 */
                    482:            (opcode & 0xFF00F0) == 0x60080 ||   /* DO/ENDO 00000110 iiiiiiii 1000hhhh */
                    483:            (opcode & 0xFFC0FF) == 0x6C000 ||   /* DO/ENDO 00000110 11DDDDDD 00000000 */
                    484:            (opcode & 0xFFC0BF) == 0x64000 ||   /* DO/ENDO 00000110 01MMMRRR 0S000000 */
                    485:            (opcode & 0xFFC0BF) == 0x60000) {   /* DO/ENDO 00000110 00aaaaaa 0S000000 */
                    486:                return CALL_BRANCH;
                    487:        }
                    488:        if (is_prev_instr(prev_pc, pc)) {
                    489:                return CALL_NEXT;
                    490:        }
                    491:        return CALL_UNKNOWN;
                    492: }
                    493: 
                    494: /**
                    495:  * If call tracking is enabled (there are symbols), collect
                    496:  * information about subroutine and other calls, and their costs.
                    497:  * 
                    498:  * Like with profile data, caller info checks need to be for previous
                    499:  * instruction, that's why "pc" argument for this function actually
                    500:  * needs to be previous PC.
                    501:  */
                    502: static void collect_calls(Uint16 pc, counters_t *counters)
                    503: {
                    504:        calltype_t flag;
                    505:        Uint16 prev_pc;
                    506:        Uint32 caller_pc;
                    507:        int idx;
                    508: 
                    509:        prev_pc = dsp_callinfo.prev_pc;
                    510:        dsp_callinfo.prev_pc = pc;
                    511:        caller_pc = PC_UNDEFINED;
                    512: 
                    513:        /* address is return address for last subroutine call? */
                    514:        if (unlikely(pc == dsp_callinfo.return_pc) && likely(dsp_callinfo.depth)) {
                    515: 
                    516:                flag = dsp_opcode_type(prev_pc, pc);
                    517:                /* return address is entered either by subroutine return,
                    518:                 * or by returning from exception that interrupted
                    519:                 * the instruction at return address.
                    520:                 */
                    521:                if (likely(flag == CALL_SUBRETURN || flag == CALL_EXCRETURN)) {
                    522:                        caller_pc = Profile_CallEnd(&dsp_callinfo, counters);
                    523:                }
                    524:        }
                    525: 
                    526:        /* address is one which we're tracking? */
1.1.1.4 ! root      527:        idx = Symbols_GetDspCodeIndex(pc);
1.1       root      528:        if (unlikely(idx >= 0)) {
                    529: 
                    530:                flag = dsp_opcode_type(prev_pc, pc);
                    531:                if (flag == CALL_SUBROUTINE) {
                    532:                        dsp_callinfo.return_pc = DSP_GetNextPC(prev_pc);  /* slow! */
                    533:                } else if (caller_pc != PC_UNDEFINED) {
                    534:                        /* returned from function, change return
                    535:                         * instruction address to address of
                    536:                         * what did the returned call.
                    537:                         */
                    538:                        prev_pc = caller_pc;
                    539:                        assert(is_prev_instr(prev_pc, pc));
                    540:                        flag = CALL_NEXT;
                    541:                }
                    542:                Profile_CallStart(idx, &dsp_callinfo, prev_pc, flag, pc, counters);
                    543: 
                    544:        }
                    545: }
                    546: 
                    547: /**
1.1.1.2   root      548:  * log last loop info, if there's suitable data for one
                    549:  */
                    550: static void log_last_loop(void)
                    551: {
                    552:        unsigned len = dsp_profile.loop_end - dsp_profile.loop_start;
                    553:        if (dsp_profile.loop_count > 1 && (len < profile_loop.dsp_limit || !profile_loop.dsp_limit)) {
                    554:                fprintf(profile_loop.fp, "DSP %d 0x%04x %d %d\n", nVBLs,
                    555:                        dsp_profile.loop_start, len, dsp_profile.loop_count);
                    556:                fflush(profile_loop.fp);
                    557:        }
                    558: }
                    559: 
                    560: /**
1.1       root      561:  * Update DSP cycle and count statistics for PC address.
                    562:  *
                    563:  * This is called after instruction is executed and PC points
                    564:  * to next instruction i.e. info is for previous PC address.
                    565:  */
                    566: void Profile_DspUpdate(void)
                    567: {
                    568:        dsp_profile_item_t *prev;
                    569:        Uint16 pc, prev_pc, cycles;
                    570:        counters_t *counters;
                    571: 
                    572:        prev_pc = dsp_profile.prev_pc;
                    573:        dsp_profile.prev_pc = pc = DSP_GetPC();
1.1.1.2   root      574: 
                    575:        if (unlikely(profile_loop.fp)) {
                    576:                if (pc < prev_pc) {
                    577:                        if (pc == dsp_profile.loop_start && prev_pc == dsp_profile.loop_end) {
                    578:                                dsp_profile.loop_count++;
                    579:                        } else {
                    580:                                dsp_profile.loop_start = pc;
                    581:                                dsp_profile.loop_end = prev_pc;
                    582:                                dsp_profile.loop_count = 1;
                    583:                        }
                    584:                } else {
                    585:                        if (pc > dsp_profile.loop_end) {
                    586:                                log_last_loop();
                    587:                                dsp_profile.loop_end = 0xFFFF;
                    588:                                dsp_profile.loop_count = 0;
                    589:                        }
                    590:                }
                    591:        }
                    592: 
1.1       root      593:        prev = dsp_profile.data + prev_pc;
                    594: 
                    595:        if (likely(prev->count < MAX_DSP_PROFILE_VALUE)) {
                    596:                prev->count++;
                    597:        }
                    598: 
                    599:        cycles = DSP_GetInstrCycles();
                    600:        if (likely(prev->cycles < MAX_DSP_PROFILE_VALUE - cycles)) {
                    601:                prev->cycles += cycles;
                    602:        } else {
                    603:                prev->cycles = MAX_DSP_PROFILE_VALUE;
                    604:        }
                    605: 
                    606:        if (unlikely(cycles < prev->min_cycle)) {
                    607:                prev->min_cycle = cycles;
                    608:        }
                    609:        if (unlikely(cycles > prev->max_cycle)) {
                    610:                prev->max_cycle = cycles;
                    611:        }
                    612: 
                    613:        counters = &(dsp_profile.ram.counters);
                    614:        if (dsp_callinfo.sites) {
                    615:                collect_calls(prev_pc, counters);
                    616:        }
                    617:        /* counters are increased after caller info is processed,
                    618:         * otherwise cost for the instruction calling the callee
                    619:         * doesn't get accounted to caller (but callee).
                    620:         */
                    621:        counters->cycles += cycles;
                    622:        counters->count++;
                    623: }
                    624: 
                    625: /**
                    626:  * Helper for collecting DSP profile area statistics.
                    627:  */
                    628: static void update_area_item(profile_area_t *area, Uint16 addr, dsp_profile_item_t *item)
                    629: {
                    630:        Uint64 cycles = item->cycles;
                    631:        Uint64 count = item->count;
                    632:        Uint16 diff;
                    633: 
                    634:        if (!count) {
                    635:                return;
                    636:        }
                    637:        if (cycles == MAX_DSP_PROFILE_VALUE) {
                    638:                area->overflow = true;
                    639:        }
                    640:        if (item->max_cycle) {
                    641:                diff = item->max_cycle - item->min_cycle;
                    642:        } else {
                    643:                diff = 0;
                    644:        }
                    645: 
                    646:        area->counters.count += count;
                    647:        area->counters.cycles += cycles;
1.1.1.3   root      648:        area->counters.cycles_diffs += diff;
1.1       root      649: 
                    650:        if (addr < area->lowest) {
                    651:                area->lowest = addr;
                    652:        }
                    653:        area->highest = addr;
                    654: 
                    655:        area->active++;
                    656: }
                    657: 
                    658: /**
                    659:  * Stop and process the DSP profiling data; collect stats and
                    660:  * prepare for more optimal sorting.
                    661:  */
                    662: void Profile_DspStop(void)
                    663: {
                    664:        dsp_profile_item_t *item;
                    665:        profile_area_t *area;
                    666:        Uint16 *sort_arr;
                    667:        Uint32 addr;
                    668: 
                    669:        if (dsp_profile.processed || !dsp_profile.enabled) {
                    670:                return;
                    671:        }
                    672: 
1.1.1.2   root      673:        log_last_loop();
                    674:        if (profile_loop.fp) {
                    675:                fflush(profile_loop.fp);
                    676:        }
                    677: 
1.1       root      678:        Profile_FinalizeCalls(&(dsp_callinfo), &(dsp_profile.ram.counters), Symbols_GetByDspAddress);
                    679: 
                    680:        /* find lowest and highest  addresses executed */
                    681:        area = &dsp_profile.ram;
                    682:        memset(area, 0, sizeof(profile_area_t));
                    683:        area->lowest = DSP_PROFILE_ARR_SIZE;
                    684: 
                    685:        item = dsp_profile.data;
                    686:        for (addr = 0; addr < DSP_PROFILE_ARR_SIZE; addr++, item++) {
                    687:                update_area_item(area, addr, item);
                    688:        }
                    689: 
                    690:        /* allocate address array for sorting */
                    691:        sort_arr = calloc(dsp_profile.ram.active, sizeof(*sort_arr));
                    692: 
                    693:        if (!sort_arr) {
                    694:                perror("ERROR: allocating DSP profile address data");
                    695:                free(dsp_profile.data);
                    696:                dsp_profile.data = NULL;
                    697:                return;
                    698:        }
                    699:        printf("Allocated DSP profile address buffer (%d KB).\n",
                    700:               (int)sizeof(*sort_arr)*(dsp_profile.ram.active+512)/1024);
                    701:        dsp_profile.sort_arr = sort_arr;
                    702: 
                    703:        /* ...and fill addresses for used instructions... */
                    704:        area = &dsp_profile.ram;
                    705:        item = &(dsp_profile.data[area->lowest]);
                    706:        for (addr = area->lowest; addr <= area->highest; addr++, item++) {
                    707:                if (item->count) {
                    708:                        *sort_arr++ = addr;
                    709:                }
                    710:        }
                    711:        //printf("%d/%d/%d\n", area->active, sort_arr-dsp_profile.sort_arr, active);
                    712: 
                    713:        Profile_DspShowStats();
                    714:        dsp_profile.processed = true;
                    715: }
                    716: 
                    717: /**
                    718:  * Get pointers to DSP profile enabling and disasm address variables
                    719:  * for updating them (in parser).
                    720:  */
                    721: void Profile_DspGetPointers(bool **enabled, Uint32 **disasm_addr)
                    722: {
                    723:        *disasm_addr = &dsp_profile.disasm_addr;
                    724:        *enabled = &dsp_profile.enabled;
                    725: }
                    726: 
                    727: /**
                    728:  * Get callinfo & symbol search pointers for stack walking.
                    729:  */
1.1.1.4 ! root      730: void Profile_DspGetCallinfo(callinfo_t **callinfo, const char* (**get_symbol)(Uint32, symtype_t))
1.1       root      731: {
                    732:        *callinfo = &(dsp_callinfo);
                    733:        *get_symbol = Symbols_GetByDspAddress;
                    734: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.