Changeset 11909ce3 in mainline


Ignore:
Timestamp:
2024-01-21T15:48:43Z (3 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
515f1b1
Parents:
33e15a0
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-03-29 10:25:36)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-21 15:48:43)
Message:

Make thread cycle statistics atomic

Location:
kernel/generic
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/proc/thread.h

    r33e15a0 r11909ce3  
    147147
    148148        /** Thread accounting. */
    149         uint64_t ucycles;
    150         uint64_t kcycles;
     149        atomic_time_stat_t ucycles;
     150        atomic_time_stat_t kcycles;
    151151        /** Last sampled cycle. */
    152152        uint64_t last_cycle;
  • kernel/generic/src/interrupt/interrupt.c

    r33e15a0 r11909ce3  
    114114
    115115        /* Account user cycles */
    116         if (THREAD) {
    117                 irq_spinlock_lock(&THREAD->lock, false);
     116        if (THREAD)
    118117                thread_update_accounting(true);
    119                 irq_spinlock_unlock(&THREAD->lock, false);
    120         }
    121118
    122119        /* Account CPU usage if it woke up from sleep */
     
    155152
    156153        /* Do not charge THREAD for exception cycles */
    157         if (THREAD) {
    158                 irq_spinlock_lock(&THREAD->lock, false);
     154        if (THREAD)
    159155                THREAD->last_cycle = end_cycle;
    160                 irq_spinlock_unlock(&THREAD->lock, false);
    161         }
    162156#else
    163157        panic("No space for any exception handler, yet we want to handle some exception.");
  • kernel/generic/src/proc/scheduler.c

    r33e15a0 r11909ce3  
    507507
    508508        /* Update thread kernel accounting */
    509         THREAD->kcycles += get_cycle() - THREAD->last_cycle;
     509        atomic_time_increment(&THREAD->kcycles, get_cycle() - THREAD->last_cycle);
    510510
    511511        /*
  • kernel/generic/src/proc/task.c

    r33e15a0 r11909ce3  
    506506        /* Current values of threads */
    507507        list_foreach(task->threads, th_link, thread_t, thread) {
    508                 irq_spinlock_lock(&thread->lock, false);
    509 
    510508                /* Process only counted threads */
    511509                if (!thread->uncounted) {
     
    515513                        }
    516514
    517                         uret += thread->ucycles;
    518                         kret += thread->kcycles;
     515                        uret += atomic_time_read(&thread->ucycles);
     516                        kret += atomic_time_read(&thread->kcycles);
    519517                }
    520 
    521                 irq_spinlock_unlock(&thread->lock, false);
    522518        }
    523519
  • kernel/generic/src/proc/thread.c

    r33e15a0 r11909ce3  
    258258        thread->thread_code = func;
    259259        thread->thread_arg = arg;
    260         thread->ucycles = 0;
    261         thread->kcycles = 0;
     260        thread->ucycles = ATOMIC_TIME_INITIALIZER();
     261        thread->kcycles = ATOMIC_TIME_INITIALIZER();
    262262        thread->uncounted =
    263263            ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
     
    333333
    334334        if (!thread->uncounted) {
    335                 thread->task->ucycles += thread->ucycles;
    336                 thread->task->kcycles += thread->kcycles;
     335                thread->task->ucycles += atomic_time_read(&thread->ucycles);
     336                thread->task->kcycles += atomic_time_read(&thread->kcycles);
    337337        }
    338338
     
    682682        uint64_t ucycles, kcycles;
    683683        char usuffix, ksuffix;
    684         order_suffix(thread->ucycles, &ucycles, &usuffix);
    685         order_suffix(thread->kcycles, &kcycles, &ksuffix);
     684        order_suffix(atomic_time_read(&thread->ucycles), &ucycles, &usuffix);
     685        order_suffix(atomic_time_read(&thread->kcycles), &kcycles, &ksuffix);
    686686
    687687        state_t state = atomic_get_unordered(&thread->state);
     
    788788void thread_update_accounting(bool user)
    789789{
     790        assert(interrupts_disabled());
     791
    790792        uint64_t time = get_cycle();
    791793
    792         assert(interrupts_disabled());
    793         assert(irq_spinlock_locked(&THREAD->lock));
    794 
    795794        if (user)
    796                 THREAD->ucycles += time - THREAD->last_cycle;
     795                atomic_time_increment(&THREAD->ucycles, time - THREAD->last_cycle);
    797796        else
    798                 THREAD->kcycles += time - THREAD->last_cycle;
     797                atomic_time_increment(&THREAD->kcycles, time - THREAD->last_cycle);
    799798
    800799        THREAD->last_cycle = time;
  • kernel/generic/src/syscall/syscall.c

    r33e15a0 r11909ce3  
    141141{
    142142        /* Do userpace accounting */
    143         irq_spinlock_lock(&THREAD->lock, true);
     143        ipl_t ipl = interrupts_disable();
    144144        thread_update_accounting(true);
    145         irq_spinlock_unlock(&THREAD->lock, true);
     145        interrupts_restore(ipl);
    146146
    147147#ifdef CONFIG_UDEBUG
     
    191191
    192192        /* Do kernel accounting */
    193         irq_spinlock_lock(&THREAD->lock, true);
     193        ipl = interrupts_disable();
    194194        thread_update_accounting(false);
    195         irq_spinlock_unlock(&THREAD->lock, true);
     195        interrupts_restore(ipl);
    196196
    197197        return rc;
  • kernel/generic/src/sysinfo/stats.c

    r33e15a0 r11909ce3  
    299299{
    300300        assert(interrupts_disabled());
    301         assert(irq_spinlock_locked(&thread->lock));
    302301
    303302        stats_thread->thread_id = thread->tid;
     
    305304        stats_thread->state = atomic_get_unordered(&thread->state);
    306305        stats_thread->priority = atomic_get_unordered(&thread->priority);
    307         stats_thread->ucycles = thread->ucycles;
    308         stats_thread->kcycles = thread->kcycles;
     306        stats_thread->ucycles = atomic_time_read(&thread->ucycles);
     307        stats_thread->kcycles = atomic_time_read(&thread->kcycles);
    309308
    310309        cpu_t *cpu = atomic_get_unordered(&thread->cpu);
Note: See TracChangeset for help on using the changeset viewer.