aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/scripts/python/futex-contention.py
blob: 11e70a388d41a5b1b348adc55df174b09394a93d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# futex contention
# (c) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
# Licensed under the terms of the GNU GPL License version 2
#
# Translation of:
#
# http://sourceware.org/systemtap/wiki/WSFutexContention
#
# to perf python scripting.
#
# Measures futex contention

import os, sys
sys.path.append(os.environ['PERF_EXEC_PATH'] + '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
from Util import *

process_names = {}
thread_thislock = {}
thread_blocktime = {}

lock_waits = {} # long-lived stats on (tid,lock) blockage elapsed time
process_names = {} # long-lived pid-to-execname mapping

def syscalls__sys_enter_futex(event, ctxt, cpu, s, ns, tid, comm,
			      nr, uaddr, op, val, utime, uaddr2, val3):
	cmd = op & FUTEX_CMD_MASK
	if cmd != FUTEX_WAIT:
		return # we don't care about originators of WAKE events

	process_names[tid] = comm
	thread_thislock[tid] = uaddr
	thread_blocktime[tid] = nsecs(s, ns)

def syscalls__sys_exit_futex(event, ctxt, cpu, s, ns, tid, comm,
			     nr, ret):
	if thread_blocktime.has_key(tid):
		elapsed = nsecs(s, ns) - thread_blocktime[tid]
		add_stats(lock_waits, (tid, thread_thislock[tid]), elapsed)
		del thread_blocktime[tid]
		del thread_thislock[tid]

def trace_begin():
	print "Press control+C to stop and show the summary"

def trace_end():
	for (tid, lock) in lock_waits:
		min, max, avg, count = lock_waits[tid, lock]
		print "%s[%d] lock %x contended %d times, %d avg ns" % \
		      (process_names[tid], tid, lock, count, avg)

a id='n394' href='#n394'>394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
/*
 * C-Brick Serial Port (and console) driver for SGI Altix machines.
 *
 * This driver is NOT suitable for talking to the l1-controller for
 * anything other than 'console activities' --- please use the l1
 * driver for that.
 *
 *
 * Copyright (c) 2004-2006 Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
 * Mountain View, CA  94043, or:
 *
 * http://www.sgi.com
 *
 * For further information regarding this notice, see:
 *
 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
 */

#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/module.h>
#include <linux/sysrq.h>
#include <linux/circ_buf.h>
#include <linux/serial_reg.h>
#include <linux/delay.h> /* for mdelay */
#include <linux/miscdevice.h>
#include <linux/serial_core.h>

#include <asm/io.h>
#include <asm/sn/simulator.h>
#include <asm/sn/sn_sal.h>

/* number of characters we can transmit to the SAL console at a time */
#define SN_SAL_MAX_CHARS 120

/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
 * avoid losing chars, (always has to be a power of 2) */
#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))

#define SN_SAL_UART_FIFO_DEPTH 16
#define SN_SAL_UART_FIFO_SPEED_CPS 9600/10

/* sn_transmit_chars() calling args */
#define TRANSMIT_BUFFERED	0
#define TRANSMIT_RAW		1

/* To use dynamic numbers only and not use the assigned major and minor,
 * define the following.. */
				  /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
#define USE_DYNAMIC_MINOR 0	/* Don't rely on misc_register dynamic minor */

/* Device name we're using */
#define DEVICE_NAME "ttySG"
#define DEVICE_NAME_DYNAMIC "ttySG0"	/* need full name for misc_register */
/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
#define DEVICE_MAJOR 204
#define DEVICE_MINOR 40

#ifdef CONFIG_MAGIC_SYSRQ
static char sysrq_serial_str[] = "\eSYS";
static char *sysrq_serial_ptr = sysrq_serial_str;
static unsigned long sysrq_requested;
#endif /* CONFIG_MAGIC_SYSRQ */

/*
 * Port definition - this kinda drives it all
 */
struct sn_cons_port {
	struct timer_list sc_timer;
	struct uart_port sc_port;
	struct sn_sal_ops {
		int (*sal_puts_raw) (const char *s, int len);
		int (*sal_puts) (const char *s, int len);
		int (*sal_getc) (void);
		int (*sal_input_pending) (void);
		void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
	} *sc_ops;
	unsigned long sc_interrupt_timeout;
	int sc_is_asynch;
};

static struct sn_cons_port sal_console_port;
static int sn_process_input;

/* Only used if USE_DYNAMIC_MINOR is set to 1 */
static struct miscdevice misc;	/* used with misc_register for dynamic */

extern void early_sn_setup(void);

#undef DEBUG
#ifdef DEBUG
static int sn_debug_printf(const char *fmt, ...);
#define DPRINTF(x...) sn_debug_printf(x)
#else
#define DPRINTF(x...) do { } while (0)
#endif

/* Prototypes */
static int snt_hw_puts_raw(const char *, int);
static int snt_hw_puts_buffered(const char *, int);
static int snt_poll_getc(void);
static int snt_poll_input_pending(void);
static int snt_intr_getc(void);
static int snt_intr_input_pending(void);
static void sn_transmit_chars(struct sn_cons_port *, int);

/* A table for polling:
 */
static struct sn_sal_ops poll_ops = {
	.sal_puts_raw = snt_hw_puts_raw,
	.sal_puts = snt_hw_puts_raw,
	.sal_getc = snt_poll_getc,
	.sal_input_pending = snt_poll_input_pending
};

/* A table for interrupts enabled */
static struct sn_sal_ops intr_ops = {
	.sal_puts_raw = snt_hw_puts_raw,
	.sal_puts = snt_hw_puts_buffered,
	.sal_getc = snt_intr_getc,
	.sal_input_pending = snt_intr_input_pending,
	.sal_wakeup_transmit = sn_transmit_chars
};

/* the console does output in two distinctly different ways:
 * synchronous (raw) and asynchronous (buffered).  initally, early_printk
 * does synchronous output.  any data written goes directly to the SAL
 * to be output (incidentally, it is internally buffered by the SAL)
 * after interrupts and timers are initialized and available for use,
 * the console init code switches to asynchronous output.  this is
 * also the earliest opportunity to begin polling for console input.
 * after console initialization, console output and tty (serial port)
 * output is buffered and sent to the SAL asynchronously (either by
 * timer callback or by UART interrupt) */

/* routines for running the console in polling mode */

/**
 * snt_poll_getc - Get a character from the console in polling mode
 *
 */
static int snt_poll_getc(void)
{
	int ch;

	ia64_sn_console_getc(&ch);
	return ch;
}

/**
 * snt_poll_input_pending - Check if any input is waiting - polling mode.
 *
 */
static int snt_poll_input_pending(void)
{
	int status, input;

	status = ia64_sn_console_check(&input);
	return !status && input;
}

/* routines for an interrupt driven console (normal) */

/**
 * snt_intr_getc - Get a character from the console, interrupt mode
 *
 */
static int snt_intr_getc(void)
{
	return ia64_sn_console_readc();
}

/**
 * snt_intr_input_pending - Check if input is pending, interrupt mode
 *
 */
static int snt_intr_input_pending(void)
{
	return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
}

/* these functions are polled and interrupt */

/**
 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
 * @s: String
 * @len: Length
 *
 */
static int snt_hw_puts_raw(const char *s, int len)
{
	/* this will call the PROM and not return until this is done */
	return ia64_sn_console_putb(s, len);
}

/**
 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
 * @s: String
 * @len: Length
 *
 */
static int snt_hw_puts_buffered(const char *s, int len)
{
	/* queue data to the PROM */
	return ia64_sn_console_xmit_chars((char *)s, len);
}

/* uart interface structs
 * These functions are associated with the uart_port that the serial core
 * infrastructure calls.
 *
 * Note: Due to how the console works, many routines are no-ops.
 */

/**
 * snp_type - What type of console are we?
 * @port: Port to operate with (we ignore since we only have one port)
 *
 */
static const char *snp_type(struct uart_port *port)
{
	return ("SGI SN L1");
}

/**
 * snp_tx_empty - Is the transmitter empty?  We pretend we're always empty
 * @port: Port to operate on (we ignore since we only have one port)
 *
 */
static unsigned int snp_tx_empty(struct uart_port *port)
{
	return 1;
}

/**
 * snp_stop_tx - stop the transmitter - no-op for us
 * @port: Port to operat eon - we ignore - no-op function
 *
 */
static void snp_stop_tx(struct uart_port *port)
{
}

/**
 * snp_release_port - Free i/o and resources for port - no-op for us
 * @port: Port to operate on - we ignore - no-op function
 *
 */
static void snp_release_port(struct uart_port *port)
{
}

/**
 * snp_enable_ms - Force modem status interrupts on - no-op for us
 * @port: Port to operate on - we ignore - no-op function
 *
 */
static void snp_enable_ms(struct uart_port *port)
{
}

/**
 * snp_shutdown - shut down the port - free irq and disable - no-op for us
 * @port: Port to shut down - we ignore
 *
 */
static void snp_shutdown(struct uart_port *port)
{
}

/**
 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
 * @port: Port to operate on - we ignore
 * @mctrl: Lines to set/unset - we ignore
 *
 */
static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
}

/**
 * snp_get_mctrl - get contorl line info, we just return a static value
 * @port: port to operate on - we only have one port so we ignore this
 *
 */
static unsigned int snp_get_mctrl(struct uart_port *port)
{
	return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
}

/**
 * snp_stop_rx - Stop the receiver - we ignor ethis
 * @port: Port to operate on - we ignore
 *
 */
static void snp_stop_rx(struct uart_port *port)
{
}

/**
 * snp_start_tx - Start transmitter
 * @port: Port to operate on
 *
 */
static void snp_start_tx(struct uart_port *port)
{
	if (sal_console_port.sc_ops->sal_wakeup_transmit)
		sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
							     TRANSMIT_BUFFERED);

}

/**
 * snp_break_ctl - handle breaks - ignored by us
 * @port: Port to operate on
 * @break_state: Break state
 *
 */
static void snp_break_ctl(struct uart_port *port, int break_state)
{
}

/**
 * snp_startup - Start up the serial port - always return 0 (We're always on)
 * @port: Port to operate on
 *
 */
static int snp_startup(struct uart_port *port)
{
	return 0;
}

/**
 * snp_set_termios - set termios stuff - we ignore these
 * @port: port to operate on
 * @termios: New settings
 * @termios: Old
 *
 */
static void
snp_set_termios(struct uart_port *port, struct ktermios *termios,
		struct ktermios *old)
{
}

/**
 * snp_request_port - allocate resources for port - ignored by us
 * @port: port to operate on
 *
 */
static int snp_request_port(struct uart_port *port)
{
	return 0;
}

/**
 * snp_config_port - allocate resources, set up - we ignore,  we're always on
 * @port: Port to operate on
 * @flags: flags used for port setup
 *
 */
static void snp_config_port(struct uart_port *port, int flags)
{
}

/* Associate the uart functions above - given to serial core */