Re: [PATCH 2/2] taskset: make threads aware

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, May 09, 2011 at 02:21:44PM -0400, Davidlohr Bueso wrote:
>  		if (sched_getaffinity(pid, cur_setsize, cur_set) < 0)
>  			err(EXIT_FAILURE, _("failed to get pid %d's affinity"), pid);
>  
> -		if (c_opt)
> -			printf(_("pid %d's current affinity list: %s\n"), pid,
> -				cpulist_create(buf, buflen, cur_set, cur_setsize));
> +		if (all_tasks) {
> +			pid_t tid;
> +			struct proc_tasks *ts = proc_open_tasks(pid);
> +
> +			if (!ts)
> +				err(EXIT_FAILURE, "cannot obtain the list of tasks");
> +			while (!proc_next_tid(ts, &tid))
> +				print_affinity("current", c_opt, tid, buf, buflen, cur_set, cur_setsize);
> +			proc_close_tasks(ts);
> +		}
>  		else
> -			printf(_("pid %d's current affinity mask: %s\n"), pid,
> -				cpumask_create(buf, buflen, cur_set, cur_setsize));
> +			print_affinity("current", c_opt, pid, buf, buflen, cur_set, cur_setsize);

sched_getaffinity(pid, ....);
print_affinity(...., tid, ....);

Do you see the bug? You have to call sched_getaffinity() for each
"tid", otherwise you print the same cpumask for all tasks.

[...]

> +	if (all_tasks) {
> +		pid_t tid;
> +		struct proc_tasks *ts = proc_open_tasks(pid);
> +
> +		if (!ts)
> +			err(EXIT_FAILURE, "cannot obtain the list of tasks");
> +		while (!proc_next_tid(ts, &tid))
> +			if (sched_setaffinity(tid, new_setsize, new_set) < 0)
> +				err(EXIT_FAILURE, _("failed to set tid %d's affinity"), tid);
> +		proc_close_tasks(ts);
> +	}
> +	else
> +		if (sched_setaffinity(pid, new_setsize, new_set) < 0)
> +			err(EXIT_FAILURE, _("failed to set pid %d's affinity"), pid);

[...]

>  	if (sched_getaffinity(pid, cur_setsize, cur_set) < 0)
>  		err(EXIT_FAILURE, _("failed to get pid %d's affinity"), pid);
>  
>  	if (pid) {
> -		if (c_opt)
> -			printf(_("pid %d's new affinity list: %s\n"), pid,
> -				cpulist_create(buf, buflen, cur_set, cur_setsize));
> +		if (all_tasks) {
> +			pid_t tid;
> +			struct proc_tasks *ts = proc_open_tasks(pid);
> +			
> +			if (!ts)
> +				err(EXIT_FAILURE, "cannot obtain the list of tasks");
> +			while (!proc_next_tid(ts, &tid))
> +				print_affinity("new", c_opt, tid, buf, buflen, cur_set, cur_setsize);
> +			proc_close_tasks(ts);
> +		}
>  		else
> -			printf(_("pid %d's new affinity mask: %s\n"), pid,
> -				cpumask_create(buf, buflen, cur_set, cur_setsize));
> +			print_affinity("new", c_opt, pid, buf, buflen, cur_set, cur_setsize);
>  	}

The another problem is that the code uses two separate loops to print
and set the masks, so it's possible that you will call sched_getaffinity()
for completely different set of tasks than sched_setaffinity(). It would 
be better to use:

do_taskset(pid_t tid, ...) 
{
        sched_getaffinity(tid, ...);     /* current */
        print_affinity(tid, ...);

        sched_setaffinity(tid, ...);     /* new */
        print_affinity(tid, ...);
}

main() 
{
        if (all_tasks) {
                struct proc_tasks *ts = proc_open_tasks(pid);
                while (!proc_next_tid(ts, &tid))
                    do_taskset(tid, ...);
                proc_close_tasks(ts);
        } else
            do_taskset(pid, ...);
}

The output will be also more readable:

 pid 100's current affinity: FOO
 pid 100's new affinity: BAR
 pid 200's current affinity: FOO
 pid 200's new affinity: BAR
 ...

Maybe chrt.c should be also fixed to use only one loop (one
proc_open_tasks()) for all operations. My bug, sorry.

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux