int sib_thr_nr;
 };
 
-static void scan_thread_topology(int *map, struct topology *t, int cpu, int *pos)
+static void scan_thread_topology(int *map, struct topology *t, int cpu,
+                                int *pos, int nr_cpus)
 {
        int i;
        int thr;
                if (!test_bit(cpu, cpumask_bits(&t->sib_thr[i])))
                        continue;
 
-               for_each_set_bit(thr,
-                                cpumask_bits(&t->sib_thr[i]),
-                                MAX_NR_CPUS)
+               for_each_set_bit(thr, cpumask_bits(&t->sib_thr[i]), nr_cpus)
                        if (map[thr] == -1)
                                map[thr] = (*pos)++;
        }
 }
 
-static void scan_core_topology(int *map, struct topology *t)
+static void scan_core_topology(int *map, struct topology *t, int nr_cpus)
 {
        int pos = 0;
        int i;
        int cpu;
 
        for (i = 0; i < t->sib_core_nr; i++)
-               for_each_set_bit(cpu,
-                                cpumask_bits(&t->sib_core[i]),
-                                MAX_NR_CPUS)
-                       scan_thread_topology(map, t, cpu, &pos);
+               for_each_set_bit(cpu, cpumask_bits(&t->sib_core[i]), nr_cpus)
+                       scan_thread_topology(map, t, cpu, &pos, nr_cpus);
 }
 
-static int str_to_bitmap(char *s, cpumask_t *b)
+static int str_to_bitmap(char *s, cpumask_t *b, int nr_cpus)
 {
        int i;
        int ret = 0;
 
        for (i = 0; i < m->nr; i++) {
                c = m->map[i];
-               if (c >= MAX_NR_CPUS) {
+               if (c >= nr_cpus) {
                        ret = -1;
                        break;
                }
 
 int svg_build_topology_map(struct perf_env *env)
 {
-       int i;
+       int i, nr_cpus;
        struct topology t;
        char *sib_core, *sib_thr;
 
+       nr_cpus = min(env->nr_cpus_online, MAX_NR_CPUS);
+
        t.sib_core_nr = env->nr_sibling_cores;
        t.sib_thr_nr = env->nr_sibling_threads;
        t.sib_core = calloc(env->nr_sibling_cores, sizeof(cpumask_t));
        }
 
        for (i = 0; i < env->nr_sibling_cores; i++) {
-               if (str_to_bitmap(sib_core, &t.sib_core[i])) {
+               if (str_to_bitmap(sib_core, &t.sib_core[i], nr_cpus)) {
                        fprintf(stderr, "topology: can't parse siblings map\n");
                        goto exit;
                }
        }
 
        for (i = 0; i < env->nr_sibling_threads; i++) {
-               if (str_to_bitmap(sib_thr, &t.sib_thr[i])) {
+               if (str_to_bitmap(sib_thr, &t.sib_thr[i], nr_cpus)) {
                        fprintf(stderr, "topology: can't parse siblings map\n");
                        goto exit;
                }
                sib_thr += strlen(sib_thr) + 1;
        }
 
-       topology_map = malloc(sizeof(int) * MAX_NR_CPUS);
+       topology_map = malloc(sizeof(int) * nr_cpus);
        if (!topology_map) {
                fprintf(stderr, "topology: no memory\n");
                goto exit;
        }
 
-       for (i = 0; i < MAX_NR_CPUS; i++)
+       for (i = 0; i < nr_cpus; i++)
                topology_map[i] = -1;
 
-       scan_core_topology(topology_map, &t);
+       scan_core_topology(topology_map, &t, nr_cpus);
 
        return 0;