Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1768 lines (1625 sloc) 46.123 kb
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 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 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
/*
* mono-perfcounters.c
*
* Performance counters support.
*
* Author: Paolo Molaro (lupus@ximian.com)
*
* Copyright 2008-2009 Novell, Inc (http://www.novell.com)
* 2011 Xamarin, Inc
*/

#include "config.h"
#include <time.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined (__OpenBSD__)
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if defined (__NetBSD__) || defined (__APPLE__)
#include <sys/sysctl.h>
#endif
#include "metadata/mono-perfcounters.h"
#include "metadata/appdomain.h"
#include "metadata/object-internals.h"
/* for mono_stats */
#include "metadata/class-internals.h"
#include "utils/mono-time.h"
#include "utils/mono-mmap.h"
#include "utils/mono-proclib.h"
#include "utils/mono-networkinterfaces.h"
#include "utils/mono-error-internals.h"
#include "utils/atomic.h"
#include <mono/io-layer/io-layer.h>

/* map of CounterSample.cs */
struct _MonoCounterSample {
gint64 rawValue;
gint64 baseValue;
gint64 counterFrequency;
gint64 systemFrequency;
gint64 timeStamp;
gint64 timeStamp100nSec;
gint64 counterTimeStamp;
int counterType;
};

#ifndef DISABLE_PERFCOUNTERS
/* map of PerformanceCounterType.cs */
enum {
NumberOfItemsHEX32=0x00000000,
NumberOfItemsHEX64=0x00000100,
NumberOfItems32=0x00010000,
NumberOfItems64=0x00010100,
CounterDelta32=0x00400400,
CounterDelta64=0x00400500,
SampleCounter=0x00410400,
CountPerTimeInterval32=0x00450400,
CountPerTimeInterval64=0x00450500,
RateOfCountsPerSecond32=0x10410400,
RateOfCountsPerSecond64=0x10410500,
RawFraction=0x20020400,
CounterTimer=0x20410500,
Timer100Ns=0x20510500,
SampleFraction=0x20C20400,
CounterTimerInverse=0x21410500,
Timer100NsInverse=0x21510500,
CounterMultiTimer=0x22410500,
CounterMultiTimer100Ns=0x22510500,
CounterMultiTimerInverse=0x23410500,
CounterMultiTimer100NsInverse=0x23510500,
AverageTimer32=0x30020400,
ElapsedTime=0x30240500,
AverageCount64=0x40020500,
SampleBase=0x40030401,
AverageBase=0x40030402,
RawBase=0x40030403,
CounterMultiBase=0x42030500
};

/* maps a small integer type to the counter types above */
static const int
simple_type_to_type [] = {
NumberOfItemsHEX32, NumberOfItemsHEX64,
NumberOfItems32, NumberOfItems64,
CounterDelta32, CounterDelta64,
SampleCounter, CountPerTimeInterval32,
CountPerTimeInterval64, RateOfCountsPerSecond32,
RateOfCountsPerSecond64, RawFraction,
CounterTimer, Timer100Ns,
SampleFraction, CounterTimerInverse,
Timer100NsInverse, CounterMultiTimer,
CounterMultiTimer100Ns, CounterMultiTimerInverse,
CounterMultiTimer100NsInverse, AverageTimer32,
ElapsedTime, AverageCount64,
SampleBase, AverageBase,
RawBase, CounterMultiBase
};

enum {
SingleInstance,
MultiInstance,
CatTypeUnknown = -1
};

enum {
ProcessInstance,
ThreadInstance,
CPUInstance,
MonoInstance,
NetworkInterfaceInstance,
CustomInstance
};

#define PERFCTR_CAT(id,name,help,type,inst,first_counter) CATEGORY_ ## id,
#define PERFCTR_COUNTER(id,name,help,type,field)
enum {
#include "mono-perfcounters-def.h"
NUM_CATEGORIES
};

#undef PERFCTR_CAT
#undef PERFCTR_COUNTER
#define PERFCTR_CAT(id,name,help,type,inst,first_counter) CATEGORY_START_ ## id = -1,
#define PERFCTR_COUNTER(id,name,help,type,field) COUNTER_ ## id,
/* each counter is assigned an id starting from 0 inside the category */
enum {
#include "mono-perfcounters-def.h"
END_COUNTERS
};

#undef PERFCTR_CAT
#undef PERFCTR_COUNTER
#define PERFCTR_CAT(id,name,help,type,inst,first_counter)
#define PERFCTR_COUNTER(id,name,help,type,field) CCOUNTER_ ## id,
/* this is used just to count the number of counters */
enum {
#include "mono-perfcounters-def.h"
NUM_COUNTERS
};

static CRITICAL_SECTION perfctr_mutex;
#define perfctr_lock() EnterCriticalSection (&perfctr_mutex)
#define perfctr_unlock() LeaveCriticalSection (&perfctr_mutex)

typedef struct {
char reserved [16];
int size;
unsigned short counters_start;
unsigned short counters_size;
unsigned short data_start;
MonoPerfCounters counters;
char data [1];
} MonoSharedArea;

/*
binary format of custom counters in shared memory, starting from MonoSharedArea* + data_start;
basic stanza:
struct stanza_header {
byte stanza_type; // FTYPE_*
byte other_info;
ushort stanza_length; // includeas header
... data ...
}

// strings are utf8
// perfcat and perfinstance are 4-bytes aligned
struct perfcat {
byte typeidx;
byte categorytype;
ushort length; // includes the counters
ushort num_counters;
ushort counters_data_size;
int num_instances;
char name[]; // null terminated
char help[]; // null terminated
// perfcounters follow
{
byte countertype;
char name[]; // null terminated
char help[]; // null terminated
}
0-byte
};

struct perfinstance {
byte typeidx;
byte data_offset; // offset of counters from beginning of struct
ushort length;
uint category_offset; // offset of category in the shared area
char name[]; // null terminated
// data follows: this is always 8-byte aligned
};

*/

enum {
FTYPE_CATEGORY = 'C',
FTYPE_DELETED = 'D',
FTYPE_PREDEF_INSTANCE = 'P', // an instance of a predef counter
FTYPE_INSTANCE = 'I',
FTYPE_DIRTY = 'd',
FTYPE_END = 0
};

typedef struct {
unsigned char ftype;
unsigned char extra;
unsigned short size;
} SharedHeader;

typedef struct {
SharedHeader header;
unsigned short num_counters;
unsigned short counters_data_size;
int num_instances;
/* variable length data follows */
char name [1];
} SharedCategory;

typedef struct {
SharedHeader header;
unsigned int category_offset;
/* variable length data follows */
char instance_name [1];
} SharedInstance;

typedef struct {
unsigned char type;
guint8 seq_num;
/* variable length data follows */
char name [1];
} SharedCounter;

typedef struct {
const char *name;
const char *help;
unsigned char id;
signed int type : 2;
unsigned int instance_type : 6;
short first_counter;
} CategoryDesc;

typedef struct {
const char *name;
const char *help;
short id;
unsigned short offset; // offset inside MonoPerfCounters
int type;
} CounterDesc;

#undef PERFCTR_CAT
#undef PERFCTR_COUNTER
#define PERFCTR_CAT(id,name,help,type,inst,first_counter) {name, help, CATEGORY_ ## id, type, inst ## Instance, CCOUNTER_ ## first_counter},
#define PERFCTR_COUNTER(id,name,help,type,field)
static const CategoryDesc
predef_categories [] = {
#include "mono-perfcounters-def.h"
{NULL, NULL, NUM_CATEGORIES, -1, 0, NUM_COUNTERS}
};

#undef PERFCTR_CAT
#undef PERFCTR_COUNTER
#define PERFCTR_CAT(id,name,help,type,inst,first_counter)
#define PERFCTR_COUNTER(id,name,help,type,field) {name, help, COUNTER_ ## id, G_STRUCT_OFFSET (MonoPerfCounters, field), type},
static const CounterDesc
predef_counters [] = {
#include "mono-perfcounters-def.h"
{NULL, NULL, -1, 0, 0}
};

/*
* We have several different classes of counters:
* *) system counters
* *) runtime counters
* *) remote counters
* *) user-defined counters
* *) windows counters (the implementation on windows will use this)
*
* To easily handle the differences we create a vtable for each class that contains the
* function pointers with the actual implementation to access the counters.
*/
typedef struct _ImplVtable ImplVtable;

typedef MonoBoolean (*SampleFunc) (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample* sample);
typedef gint64 (*UpdateFunc) (ImplVtable *vtable, MonoBoolean do_incr, gint64 value);
typedef void (*CleanupFunc) (ImplVtable *vtable);

struct _ImplVtable {
void *arg;
SampleFunc sample;
UpdateFunc update;
CleanupFunc cleanup;
};

typedef struct {
int id;
char *name;
} NetworkVtableArg;

typedef struct {
ImplVtable vtable;
MonoPerfCounters *counters;
int pid;
} PredefVtable;

typedef struct {
ImplVtable vtable;
SharedInstance *instance_desc;
SharedCounter *counter_desc;
} CustomVTable;

static ImplVtable*
create_vtable (void *arg, SampleFunc sample, UpdateFunc update)
{
ImplVtable *vtable = g_new0 (ImplVtable, 1);
vtable->arg = arg;
vtable->sample = sample;
vtable->update = update;
return vtable;
}

MonoPerfCounters *mono_perfcounters = NULL;
static MonoSharedArea *shared_area = NULL;

typedef struct {
void *sarea;
int refcount;
} ExternalSArea;

/* maps a pid to a ExternalSArea pointer */
static GHashTable *pid_to_shared_area = NULL;

static MonoSharedArea *
load_sarea_for_pid (int pid)
{
ExternalSArea *data;
MonoSharedArea *area = NULL;

perfctr_lock ();
if (pid_to_shared_area == NULL)
pid_to_shared_area = g_hash_table_new (NULL, NULL);
data = g_hash_table_lookup (pid_to_shared_area, GINT_TO_POINTER (pid));
if (!data) {
area = mono_shared_area_for_pid (GINT_TO_POINTER (pid));
if (area) {
data = g_new (ExternalSArea, 1);
data->sarea = area;
data->refcount = 1;
g_hash_table_insert (pid_to_shared_area, GINT_TO_POINTER (pid), data);
}
} else {
area = data->sarea;
data->refcount ++;
}
perfctr_unlock ();
return area;
}

static void
unref_pid_unlocked (int pid)
{
ExternalSArea *data;
data = g_hash_table_lookup (pid_to_shared_area, GINT_TO_POINTER (pid));
if (data) {
data->refcount--;
if (!data->refcount) {
g_hash_table_remove (pid_to_shared_area, GINT_TO_POINTER (pid));
mono_shared_area_unload (data->sarea);
g_free (data);
}
}
}

static void
predef_cleanup (ImplVtable *vtable)
{
PredefVtable *vt = (PredefVtable*)vtable;
/* ExternalSArea *data; */

perfctr_lock ();
if (!pid_to_shared_area) {
perfctr_unlock ();
return;
}
unref_pid_unlocked (vt->pid);
perfctr_unlock ();
}

static guint64
mono_determine_physical_ram_size (void)
{
#if defined (TARGET_WIN32)
MEMORYSTATUSEX memstat;

memstat.dwLength = sizeof (memstat);
GlobalMemoryStatusEx (&memstat);
return (guint64)memstat.ullTotalPhys;
#elif defined (__NetBSD__) || defined (__APPLE__)
#ifdef __NetBSD__
unsigned long value;
#else
guint64 value;
#endif
int mib[2] = {
CTL_HW,
#ifdef __NetBSD__
HW_PHYSMEM
#else
HW_MEMSIZE
#endif
};
size_t size_sys = sizeof (value);

sysctl (mib, 2, &value, &size_sys, NULL, 0);
if (value == 0)
return 134217728;

return (guint64)value;
#elif defined (HAVE_SYSCONF)
guint64 page_size = 0, num_pages = 0;

/* sysconf works on most *NIX operating systems, if your system doesn't have it or if it
* reports invalid values, please add your OS specific code below. */
#ifdef _SC_PAGESIZE
page_size = (guint64)sysconf (_SC_PAGESIZE);
#endif

#ifdef _SC_PHYS_PAGES
num_pages = (guint64)sysconf (_SC_PHYS_PAGES);
#endif

if (!page_size || !num_pages) {
g_warning ("Your operating system's sysconf (3) function doesn't correctly report physical memory size!");
return 134217728;
}

return page_size * num_pages;
#else
return 134217728;
#endif
}

void
mono_perfcounters_init (void)
{
int d_offset = G_STRUCT_OFFSET (MonoSharedArea, data);
d_offset += 7;
d_offset &= ~7;

InitializeCriticalSection (&perfctr_mutex);

shared_area = mono_shared_area ();
shared_area->counters_start = G_STRUCT_OFFSET (MonoSharedArea, counters);
shared_area->counters_size = sizeof (MonoPerfCounters);
shared_area->data_start = d_offset;
shared_area->size = 4096;
mono_perfcounters = &shared_area->counters;
}

static int
perfctr_type_compress (int type)
{
int i;
for (i = 0; i < G_N_ELEMENTS (simple_type_to_type); ++i) {
if (simple_type_to_type [i] == type)
return i;
}
/* NumberOfItems32 */
return 2;
}

static unsigned char*
shared_data_find_room (int size)
{
unsigned char *p = (unsigned char *)shared_area + shared_area->data_start;
unsigned char *end = (unsigned char *)shared_area + shared_area->size;

size += 7;
size &= ~7;
while (p < end) {
unsigned short *next;
if (*p == FTYPE_END) {
if (size < (end - p))
return p;
return NULL;
}
if (p + 4 > end)
return NULL;
next = (unsigned short*)(p + 2);
if (*p == FTYPE_DELETED) {
/* we reuse only if it's the same size */
if (*next == size) {
return p;
}
}
p += *next;
}
return NULL;
}

typedef gboolean (*SharedFunc) (SharedHeader *header, void *data);

static void
foreach_shared_item_in_area (unsigned char *p, unsigned char *end, SharedFunc func, void *data)
{
while (p < end) {
unsigned short *next;
if (p + 4 > end)
return;
next = (unsigned short*)(p + 2);
if (!func ((SharedHeader*)p, data))
return;
if (*p == FTYPE_END)
return;
p += *next;
}
}

static void
foreach_shared_item (SharedFunc func, void *data)
{
unsigned char *p = (unsigned char *)shared_area + shared_area->data_start;
unsigned char *end = (unsigned char *)shared_area + shared_area->size;

foreach_shared_item_in_area (p, end, func, data);
}

static int
mono_string_compare_ascii (MonoString *str, const char *ascii_str)
{
/* FIXME: make this case insensitive */
guint16 *strc = mono_string_chars (str);
while (*strc == *ascii_str++) {
if (*strc == 0)
return 0;
strc++;
}
return *strc - *(const unsigned char *)(ascii_str - 1);
}

typedef struct {
MonoString *name;
SharedCategory *cat;
} CatSearch;

static gboolean
category_search (SharedHeader *header, void *data)
{
CatSearch *search = data;
if (header->ftype == FTYPE_CATEGORY) {
SharedCategory *cat = (SharedCategory*)header;
if (mono_string_compare_ascii (search->name, cat->name) == 0) {
search->cat = cat;
return FALSE;
}
}
return TRUE;
}

static SharedCategory*
find_custom_category (MonoString *name)
{
CatSearch search;
search.name = name;
search.cat = NULL;
foreach_shared_item (category_search, &search);
return search.cat;
}

static gboolean
category_collect (SharedHeader *header, void *data)
{
GSList **list = data;
if (header->ftype == FTYPE_CATEGORY) {
*list = g_slist_prepend (*list, header);
}
return TRUE;
}

static GSList*
get_custom_categories (void) {
GSList *list = NULL;
foreach_shared_item (category_collect, &list);
return list;
}

static char*
custom_category_counters (SharedCategory* cat)
{
char *p = cat->name + strlen (cat->name) + 1;
p += strlen (p) + 1; /* skip category help */
return p;
}

static SharedCounter*
find_custom_counter (SharedCategory* cat, MonoString *name)
{
int i;
char *p = custom_category_counters (cat);
for (i = 0; i < cat->num_counters; ++i) {
SharedCounter *counter = (SharedCounter*)p;
if (mono_string_compare_ascii (name, counter->name) == 0)
return counter;
p += 1 + strlen (p + 1) + 1; /* skip counter type and name */
p += strlen (p) + 1; /* skip counter help */
}
return NULL;
}

typedef struct {
unsigned int cat_offset;
SharedCategory* cat;
MonoString *instance;
SharedInstance* result;
GSList *list;
} InstanceSearch;

static gboolean
instance_search (SharedHeader *header, void *data)
{
InstanceSearch *search = data;
if (header->ftype == FTYPE_INSTANCE) {
SharedInstance *ins = (SharedInstance*)header;
if (search->cat_offset == ins->category_offset) {
if (search->instance) {
if (mono_string_compare_ascii (search->instance, ins->instance_name) == 0) {
search->result = ins;
return FALSE;
}
} else {
search->list = g_slist_prepend (search->list, ins);
}
}
}
return TRUE;
}

static SharedInstance*
find_custom_instance (SharedCategory* cat, MonoString *instance)
{
InstanceSearch search;
search.cat_offset = (char*)cat - (char*)shared_area;
search.cat = cat;
search.instance = instance;
search.list = NULL;
search.result = NULL;
foreach_shared_item (instance_search, &search);
return search.result;
}

static GSList*
get_custom_instances_list (SharedCategory* cat)
{
InstanceSearch search;
search.cat_offset = (char*)cat - (char*)shared_area;
search.cat = cat;
search.instance = NULL;
search.list = NULL;
search.result = NULL;
foreach_shared_item (instance_search, &search);
return search.list;
}

static char*
custom_category_help (SharedCategory* cat)
{
return cat->name + strlen (cat->name) + 1;
}

static const CounterDesc*
get_counter_in_category (const CategoryDesc *desc, MonoString *counter)
{
const CounterDesc *cdesc = &predef_counters [desc->first_counter];
const CounterDesc *end = &predef_counters [desc [1].first_counter];
for (; cdesc < end; ++cdesc) {
if (mono_string_compare_ascii (counter, cdesc->name) == 0)
return cdesc;
}
return NULL;
}

/* fill the info in sample (except the raw value) */
static void
fill_sample (MonoCounterSample *sample)
{
sample->timeStamp = mono_100ns_ticks ();
sample->timeStamp100nSec = sample->timeStamp;
sample->counterTimeStamp = sample->timeStamp;
sample->counterFrequency = 10000000;
sample->systemFrequency = 10000000;
// the real basevalue needs to be get from a different counter...
sample->baseValue = 0;
}

static int
id_from_string (MonoString *instance, gboolean is_process)
{
int id = -1;
if (mono_string_length (instance)) {
char *id_str = mono_string_to_utf8 (instance);
char *end;
id = strtol (id_str, &end, 0);
if (end == id_str && !is_process)
id = -1;
g_free (id_str);
}
return id;
}

static MonoBoolean
get_cpu_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
MonoProcessError error;
int id = GPOINTER_TO_INT (vtable->arg);
int pid = id >> 5;
id &= 0x1f;
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
sample->counterType = predef_counters [predef_categories [CATEGORY_CPU].first_counter + id].type;
switch (id) {
case COUNTER_CPU_USER_TIME:
sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_USER_TIME, &error);
return TRUE;
case COUNTER_CPU_PRIV_TIME:
sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_PRIV_TIME, &error);
return TRUE;
case COUNTER_CPU_INTR_TIME:
sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_INTR_TIME, &error);
return TRUE;
case COUNTER_CPU_DCP_TIME:
sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_DCP_TIME, &error);
return TRUE;
case COUNTER_CPU_PROC_TIME:
sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_IDLE_TIME, &error);
return TRUE;
}
return FALSE;
}

static void*
cpu_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
{
int id = id_from_string (instance, FALSE) << 5;
const CounterDesc *cdesc;
*custom = FALSE;
/* increase the shift above and the mask also in the implementation functions */
//g_assert (32 > desc [1].first_counter - desc->first_counter);
if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_CPU], counter))) {
*type = cdesc->type;
return create_vtable (GINT_TO_POINTER (id | cdesc->id), get_cpu_counter, NULL);
}
return NULL;
}

static MonoBoolean
get_network_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
MonoNetworkError error = MONO_NETWORK_ERROR_OTHER;
NetworkVtableArg *narg = (NetworkVtableArg*) vtable->arg;
if (!only_value) {
fill_sample (sample);
}

sample->counterType = predef_counters [predef_categories [CATEGORY_NETWORK].first_counter + narg->id].type;
switch (narg->id) {
case COUNTER_NETWORK_BYTESRECSEC:
sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESREC, &error);
break;
case COUNTER_NETWORK_BYTESSENTSEC:
sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESSENT, &error);
break;
case COUNTER_NETWORK_BYTESTOTALSEC:
sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESTOTAL, &error);
break;
}

if (error == MONO_NETWORK_ERROR_NONE)
return TRUE;
else
return FALSE;
}

static void
network_cleanup (ImplVtable *vtable)
{
NetworkVtableArg *narg;

if (vtable == NULL)
return;

narg = vtable->arg;
if (narg == NULL)
return;

g_free (narg->name);
narg->name = NULL;
g_free (narg);
vtable->arg = NULL;
}

static void*
network_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
{
const CounterDesc *cdesc;
NetworkVtableArg *narg;
ImplVtable *vtable;
char *instance_name;

*custom = FALSE;
if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_NETWORK], counter))) {
instance_name = mono_string_to_utf8 (instance);
narg = g_new0 (NetworkVtableArg, 1);
narg->id = cdesc->id;
narg->name = instance_name;
*type = cdesc->type;
vtable = create_vtable (narg, get_network_counter, NULL);
vtable->cleanup = network_cleanup;
return vtable;
}
return NULL;
}

static MonoBoolean
get_process_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
int id = GPOINTER_TO_INT (vtable->arg);
int pid = id >> 5;
if (pid < 0)
return FALSE;
id &= 0x1f;
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
sample->counterType = predef_counters [predef_categories [CATEGORY_PROC].first_counter + id].type;
switch (id) {
case COUNTER_PROC_USER_TIME:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_USER_TIME);
return TRUE;
case COUNTER_PROC_PRIV_TIME:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_SYSTEM_TIME);
return TRUE;
case COUNTER_PROC_PROC_TIME:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_TOTAL_TIME);
return TRUE;
case COUNTER_PROC_THREADS:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_NUM_THREADS);
return TRUE;
case COUNTER_PROC_VBYTES:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_VIRTUAL_BYTES);
return TRUE;
case COUNTER_PROC_WSET:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_WORKING_SET);
return TRUE;
case COUNTER_PROC_PBYTES:
sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_PRIVATE_BYTES);
return TRUE;
}
return FALSE;
}

static void*
process_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
{
int id = id_from_string (instance, TRUE) << 5;
const CounterDesc *cdesc;
*custom = FALSE;
/* increase the shift above and the mask also in the implementation functions */
//g_assert (32 > desc [1].first_counter - desc->first_counter);
if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_PROC], counter))) {
*type = cdesc->type;
return create_vtable (GINT_TO_POINTER (id | cdesc->id), get_process_counter, NULL);
}
return NULL;
}

static MonoBoolean
mono_mem_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
int id = GPOINTER_TO_INT (vtable->arg);
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
sample->counterType = predef_counters [predef_categories [CATEGORY_MONO_MEM].first_counter + id].type;
switch (id) {
case COUNTER_MEM_NUM_OBJECTS:
sample->rawValue = mono_stats.new_object_count;
return TRUE;
case COUNTER_MEM_PHYS_TOTAL:
sample->rawValue = mono_determine_physical_ram_size ();;
return TRUE;
}
return FALSE;
}

static void*
mono_mem_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
{
const CounterDesc *cdesc;
*custom = FALSE;
if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_MONO_MEM], counter))) {
*type = cdesc->type;
return create_vtable (GINT_TO_POINTER ((gint) cdesc->id), mono_mem_counter, NULL);
}
return NULL;
}

static MonoBoolean
predef_readonly_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
PredefVtable *vt = (PredefVtable *)vtable;
const CounterDesc *desc;
int cat_id = GPOINTER_TO_INT (vtable->arg);
int id = cat_id >> 16;
cat_id &= 0xffff;
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
desc = &predef_counters [predef_categories [cat_id].first_counter + id];
sample->counterType = desc->type;
/* FIXME: check that the offset fits inside imported counters */
/*g_print ("loading %s at %d\n", desc->name, desc->offset);*/
sample->rawValue = *(guint32*)((char*)vt->counters + desc->offset);
return TRUE;
}

static ImplVtable*
predef_vtable (void *arg, MonoString *instance)
{
MonoSharedArea *area;
PredefVtable *vtable;
char *pids = mono_string_to_utf8 (instance);
int pid;

pid = atoi (pids);
g_free (pids);
area = load_sarea_for_pid (pid);
if (!area)
return NULL;

vtable = g_new (PredefVtable, 1);
vtable->vtable.arg = arg;
vtable->vtable.sample = predef_readonly_counter;
vtable->vtable.cleanup = predef_cleanup;
vtable->counters = (MonoPerfCounters*)((char*)area + area->counters_start);
vtable->pid = pid;

return (ImplVtable*)vtable;
}

/* consider storing the pointer directly in vtable->arg, so the runtime overhead is lower:
* this needs some way to set sample->counterType as well, though.
*/
static MonoBoolean
predef_writable_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
int cat_id = GPOINTER_TO_INT (vtable->arg);
int id = cat_id >> 16;
cat_id &= 0xffff;
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
sample->counterType = predef_counters [predef_categories [cat_id].first_counter + id].type;
switch (cat_id) {
case CATEGORY_EXC:
switch (id) {
case COUNTER_EXC_THROWN:
sample->rawValue = mono_perfcounters->exceptions_thrown;
return TRUE;
}
break;
case CATEGORY_ASPNET:
switch (id) {
case COUNTER_ASPNET_REQ_Q:
sample->rawValue = mono_perfcounters->aspnet_requests_queued;
return TRUE;
case COUNTER_ASPNET_REQ_TOTAL:
sample->rawValue = mono_perfcounters->aspnet_requests;
return TRUE;
}
break;
case CATEGORY_THREADPOOL:
switch (id) {
case COUNTER_THREADPOOL_WORKITEMS:
sample->rawValue = mono_perfcounters->threadpool_workitems;
return TRUE;
case COUNTER_THREADPOOL_IOWORKITEMS:
sample->rawValue = mono_perfcounters->threadpool_ioworkitems;
return TRUE;
case COUNTER_THREADPOOL_THREADS:
sample->rawValue = mono_perfcounters->threadpool_threads;
return TRUE;
case COUNTER_THREADPOOL_IOTHREADS:
sample->rawValue = mono_perfcounters->threadpool_iothreads;
return TRUE;
}
break;
case CATEGORY_JIT:
switch (id) {
case COUNTER_JIT_BYTES:
sample->rawValue = mono_perfcounters->jit_bytes;
return TRUE;
case COUNTER_JIT_METHODS:
sample->rawValue = mono_perfcounters->jit_methods;
return TRUE;
case COUNTER_JIT_TIME:
sample->rawValue = mono_perfcounters->jit_time;
return TRUE;
case COUNTER_JIT_BYTES_PSEC:
sample->rawValue = mono_perfcounters->jit_bytes;
return TRUE;
case COUNTER_JIT_FAILURES:
sample->rawValue = mono_perfcounters->jit_failures;
return TRUE;
}
break;
}
return FALSE;
}

static gint64
predef_writable_update (ImplVtable *vtable, MonoBoolean do_incr, gint64 value)
{
guint32 *volatile ptr = NULL;
gint64 *volatile ptr64 = NULL;
int cat_id = GPOINTER_TO_INT (vtable->arg);
int id = cat_id >> 16;
cat_id &= 0xffff;
switch (cat_id) {
case CATEGORY_ASPNET:
switch (id) {
case COUNTER_ASPNET_REQ_Q: ptr = &mono_perfcounters->aspnet_requests_queued; break;
case COUNTER_ASPNET_REQ_TOTAL: ptr = &mono_perfcounters->aspnet_requests; break;
}
break;
case CATEGORY_THREADPOOL:
switch (id) {
case COUNTER_THREADPOOL_WORKITEMS: ptr64 = (gint64 *) &mono_perfcounters->threadpool_workitems; break;
case COUNTER_THREADPOOL_IOWORKITEMS: ptr64 = (gint64 *) &mono_perfcounters->threadpool_ioworkitems; break;
case COUNTER_THREADPOOL_THREADS: ptr = &mono_perfcounters->threadpool_threads; break;
case COUNTER_THREADPOOL_IOTHREADS: ptr = &mono_perfcounters->threadpool_iothreads; break;
}
break;
}
if (ptr) {
if (do_incr) {
if (value == 1)
return InterlockedIncrement ((gint32 *) ptr); /* FIXME: sign */
if (value == -1)
return InterlockedDecrement ((gint32 *) ptr); /* FIXME: sign */

*ptr += value;
return *ptr;
}
/* this can be non-atomic */
*ptr = value;
return value;
} else if (ptr64) {
if (do_incr) {
/* FIXME: we need to do this atomically */
/* No InterlockedIncrement64() yet */
/*
if (value == 1)
return InterlockedIncrement64 (ptr);
if (value == -1)
return InterlockedDecrement64 (ptr);
*/

*ptr64 += value;
return *ptr64;
}
/* this can be non-atomic */
*ptr64 = value;
return value;
}
return 0;
}

static void*
predef_writable_get_impl (int cat, MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
{
const CounterDesc *cdesc;
*custom = TRUE;
if ((cdesc = get_counter_in_category (&predef_categories [cat], counter))) {
*type = cdesc->type;
if (instance == NULL || mono_string_compare_ascii (instance, "") == 0)
return create_vtable (GINT_TO_POINTER ((cdesc->id << 16) | cat), predef_writable_counter, predef_writable_update);
else
return predef_vtable (GINT_TO_POINTER ((cdesc->id << 16) | cat), instance);
}
return NULL;
}

static MonoBoolean
custom_writable_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
{
CustomVTable *counter_data = (CustomVTable *)vtable;
if (!only_value) {
fill_sample (sample);
sample->baseValue = 1;
}
sample->counterType = simple_type_to_type [counter_data->counter_desc->type];
if (!vtable->arg)
sample->rawValue = 0;
else
sample->rawValue = *(guint64*)vtable->arg;
return TRUE;
}

static gint64
custom_writable_update (ImplVtable *vtable, MonoBoolean do_incr, gint64 value)
{
/* FIXME: check writability */
guint64 *ptr = vtable->arg;
if (ptr) {
if (do_incr) {
/* FIXME: we need to do this atomically */
*ptr += value;
return *ptr;
}
/* this can be non-atomic */
*ptr = value;
return value;
}
return 0;
}

static SharedInstance*
custom_get_instance (SharedCategory *cat, SharedCounter *scounter, MonoString* instance)
{
SharedInstance* inst;
unsigned char *ptr;
char *p;
int size, data_offset;
char *name;
inst = find_custom_instance (cat, instance);
if (inst)
return inst;
name = mono_string_to_utf8 (instance);
size = sizeof (SharedInstance) + strlen (name);
size += 7;
size &= ~7;
data_offset = size;
size += (sizeof (guint64) * cat->num_counters);
perfctr_lock ();
ptr = shared_data_find_room (size);
if (!ptr) {
perfctr_unlock ();
g_free (name);
return NULL;
}
inst = (SharedInstance*)ptr;
inst->header.extra = 0; /* data_offset could overflow here, so we leave this field unused */
inst->header.size = size;
inst->category_offset = (char*)cat - (char*)shared_area;
cat->num_instances++;
/* now copy the variable data */
p = inst->instance_name;
strcpy (p, name);
p += strlen (name) + 1;
inst->header.ftype = FTYPE_INSTANCE;
perfctr_unlock ();
g_free (name);

return inst;
}

static ImplVtable*
custom_vtable (SharedCounter *scounter, SharedInstance* inst, char *data)
{
CustomVTable* vtable;
vtable = g_new0 (CustomVTable, 1);
vtable->vtable.arg = data;
vtable->vtable.sample = custom_writable_counter;
vtable->vtable.update = custom_writable_update;
vtable->instance_desc = inst;
vtable->counter_desc = scounter;

return (ImplVtable*)vtable;
}

static void*
custom_get_impl (SharedCategory *cat, MonoString* counter, MonoString* instance, int *type)
{
SharedCounter *scounter;
SharedInstance* inst;
int size;

scounter = find_custom_counter (cat, counter);
if (!scounter)
return NULL;
*type = simple_type_to_type [scounter->type];
inst = custom_get_instance (cat, scounter, instance);
if (!inst)
return NULL;
size = sizeof (SharedInstance) + strlen (inst->instance_name);
size += 7;
size &= ~7;
return custom_vtable (scounter, inst, (char*)inst + size + scounter->seq_num * sizeof (guint64));
}

static const CategoryDesc*
find_category (MonoString *category)
{
int i;
for (i = 0; i < NUM_CATEGORIES; ++i) {
if (mono_string_compare_ascii (category, predef_categories [i].name) == 0)
return &predef_categories [i];
}
return NULL;
}

void*
mono_perfcounter_get_impl (MonoString* category, MonoString* counter, MonoString* instance,
MonoString* machine, int *type, MonoBoolean *custom)
{
const CategoryDesc *cdesc;
/* no support for counters on other machines */
if (mono_string_compare_ascii (machine, "."))
return NULL;
cdesc = find_category (category);
if (!cdesc) {
SharedCategory *scat = find_custom_category (category);
if (!scat)
return NULL;
*custom = TRUE;
return custom_get_impl (scat, counter, instance, type);
}
switch (cdesc->id) {
case CATEGORY_CPU:
return cpu_get_impl (counter, instance, type, custom);
case CATEGORY_PROC:
return process_get_impl (counter, instance, type, custom);
case CATEGORY_MONO_MEM:
return mono_mem_get_impl (counter, instance, type, custom);
case CATEGORY_NETWORK:
return network_get_impl (counter, instance, type, custom);
case CATEGORY_JIT:
case CATEGORY_EXC:
case CATEGORY_GC:
case CATEGORY_REMOTING:
case CATEGORY_LOADING:
case CATEGORY_THREAD:
case CATEGORY_INTEROP:
case CATEGORY_SECURITY:
case CATEGORY_ASPNET:
case CATEGORY_THREADPOOL:
return predef_writable_get_impl (cdesc->id, counter, instance, type, custom);
}
return NULL;
}

MonoBoolean
mono_perfcounter_get_sample (void *impl, MonoBoolean only_value, MonoCounterSample *sample)
{
ImplVtable *vtable = impl;
if (vtable && vtable->sample)
return vtable->sample (vtable, only_value, sample);
return FALSE;
}

gint64
mono_perfcounter_update_value (void *impl, MonoBoolean do_incr, gint64 value)
{
ImplVtable *vtable = impl;
if (vtable && vtable->update)
return vtable->update (vtable, do_incr, value);
return 0;
}

void
mono_perfcounter_free_data (void *impl)
{
ImplVtable *vtable = impl;
if (vtable && vtable->cleanup)
vtable->cleanup (vtable);
g_free (impl);
}

/* Category icalls */
MonoBoolean
mono_perfcounter_category_del (MonoString *name)
{
const CategoryDesc *cdesc;
SharedCategory *cat;
cdesc = find_category (name);
/* can't delete a predefined category */
if (cdesc)
return FALSE;
perfctr_lock ();
cat = find_custom_category (name);
/* FIXME: check the semantics, if deleting a category means also deleting the instances */
if (!cat || cat->num_instances) {
perfctr_unlock ();
return FALSE;
}
cat->header.ftype = FTYPE_DELETED;
perfctr_unlock ();
return TRUE;
}

MonoString*
mono_perfcounter_category_help (MonoString *category, MonoString *machine)
{
const CategoryDesc *cdesc;
/* no support for counters on other machines */
if (mono_string_compare_ascii (machine, "."))
return NULL;
cdesc = find_category (category);
if (!cdesc) {
SharedCategory *scat = find_custom_category (category);
if (!scat)
return NULL;
return mono_string_new (mono_domain_get (), custom_category_help (scat));
}
return mono_string_new (mono_domain_get (), cdesc->help);
}

/*
* Check if the category named @category exists on @machine. If @counter is not NULL, return
* TRUE only if a counter with that name exists in the category.
*/
MonoBoolean
mono_perfcounter_category_exists (MonoString *counter, MonoString *category, MonoString *machine)
{
const CategoryDesc *cdesc;
/* no support for counters on other machines */
if (mono_string_compare_ascii (machine, "."))
return FALSE;
cdesc = find_category (category);
if (!cdesc) {
SharedCategory *scat = find_custom_category (category);
if (!scat)
return FALSE;
/* counter is allowed to be null */
if (!counter)
return TRUE;
/* search through the custom category */
return find_custom_counter (scat, counter) != NULL;
}
/* counter is allowed to be null */
if (!counter)
return TRUE;
if (get_counter_in_category (cdesc, counter))
return TRUE;
return FALSE;
}

/* C map of the type with the same name */
typedef struct {
MonoObject object;
MonoString *help;
MonoString *name;
int type;
} CounterCreationData;

/*
* Since we'll keep a copy of the category per-process, we should also make sure
* categories with the same name are compatible.
*/
MonoBoolean
mono_perfcounter_create (MonoString *category, MonoString *help, int type, MonoArray *items)
{
MonoError error;
int result = FALSE;
int i, size;
int num_counters = mono_array_length (items);
int counters_data_size;
char *name = NULL;
char *chelp = NULL;
char **counter_info = NULL;
unsigned char *ptr;
char *p;
SharedCategory *cat;

/* FIXME: ensure there isn't a category created already */
mono_error_init (&error);
name = mono_string_to_utf8_checked (category, &error);
if (!mono_error_ok (&error))
goto failure;
chelp = mono_string_to_utf8_checked (help, &error);
if (!mono_error_ok (&error))
goto failure;
counter_info = g_new0 (char*, num_counters * 2);
/* calculate the size we need structure size + name/help + 2 0 string terminators */
size = G_STRUCT_OFFSET (SharedCategory, name) + strlen (name) + strlen (chelp) + 2;
for (i = 0; i < num_counters; ++i) {
CounterCreationData *data = mono_array_get (items, CounterCreationData*, i);
counter_info [i * 2] = mono_string_to_utf8_checked (data->name, &error);
if (!mono_error_ok (&error))
goto failure;
counter_info [i * 2 + 1] = mono_string_to_utf8_checked (data->help, &error);
if (!mono_error_ok (&error))
goto failure;
size += sizeof (SharedCounter) + 1; /* 1 is for the help 0 terminator */
}
for (i = 0; i < num_counters * 2; ++i) {
if (!counter_info [i])
goto failure;
size += strlen (counter_info [i]) + 1;
}
size += 7;
size &= ~7;
counters_data_size = num_counters * 8; /* optimize for size later */
if (size > 65535)
goto failure;
perfctr_lock ();
ptr = shared_data_find_room (size);
if (!ptr) {
perfctr_unlock ();
goto failure;
}
cat = (SharedCategory*)ptr;
cat->header.extra = type;
cat->header.size = size;
cat->num_counters = num_counters;
cat->counters_data_size = counters_data_size;
/* now copy the vaiable data */
p = cat->name;
strcpy (p, name);
p += strlen (name) + 1;
strcpy (p, chelp);
p += strlen (chelp) + 1;
for (i = 0; i < num_counters; ++i) {
CounterCreationData *data = mono_array_get (items, CounterCreationData*, i);
/* emit the SharedCounter structures */
*p++ = perfctr_type_compress (data->type);
*p++ = i;
strcpy (p, counter_info [i * 2]);
p += strlen (counter_info [i * 2]) + 1;
strcpy (p, counter_info [i * 2 + 1]);
p += strlen (counter_info [i * 2 + 1]) + 1;
}
cat->header.ftype = FTYPE_CATEGORY;

perfctr_unlock ();
result = TRUE;
failure:
if (counter_info) {
for (i = 0; i < num_counters * 2; ++i) {
g_free (counter_info [i]);
}
g_free (counter_info);
}
g_free (name);
g_free (chelp);
mono_error_cleanup (&error);
return result;
}

int
mono_perfcounter_instance_exists (MonoString *instance, MonoString *category, MonoString *machine)
{
const CategoryDesc *cdesc;
/* no support for counters on other machines */
/*FIXME: machine appears to be wrong
if (mono_string_compare_ascii (machine, "."))
return FALSE;*/
cdesc = find_category (category);
if (!cdesc) {
SharedCategory *scat;
scat = find_custom_category (category);
if (!scat)
return FALSE;
if (find_custom_instance (scat, instance))
return TRUE;
} else {
/* FIXME: search instance */
}
return FALSE;
}

MonoArray*
mono_perfcounter_category_names (MonoString *machine)
{
int i;
MonoArray *res;
MonoDomain *domain = mono_domain_get ();
GSList *custom_categories, *tmp;
/* no support for counters on other machines */
if (mono_string_compare_ascii (machine, "."))
return mono_array_new (domain, mono_get_string_class (), 0);
perfctr_lock ();
custom_categories = get_custom_categories ();
res = mono_array_new (domain, mono_get_string_class (), NUM_CATEGORIES + g_slist_length (custom_categories));
for (i = 0; i < NUM_CATEGORIES; ++i) {
const CategoryDesc *cdesc = &predef_categories [i];
mono_array_setref (res, i, mono_string_new (domain, cdesc->name));
}
for (tmp = custom_categories; tmp; tmp = tmp->next) {
SharedCategory *scat = tmp->data;
mono_array_setref (res, i, mono_string_new (domain, scat->name));
i++;
}
perfctr_unlock ();
g_slist_free (custom_categories);
return res;
}

MonoArray*
mono_perfcounter_counter_names (MonoString *category, MonoString *machine)
{
int i;
SharedCategory *scat;
const CategoryDesc *cdesc;
MonoArray *res;
MonoDomain *domain = mono_domain_get ();
/* no support for counters on other machines */
if (mono_string_compare_ascii (machine, "."))
return mono_array_new (domain, mono_get_string_class (), 0);
cdesc = find_category (category);
if (cdesc) {
res = mono_array_new (domain, mono_get_string_class (), cdesc [1].first_counter - cdesc->first_counter);
for (i = cdesc->first_counter; i < cdesc [1].first_counter; ++i) {
const CounterDesc *desc = &predef_counters [i];
mono_array_setref (res, i - cdesc->first_counter, mono_string_new (domain, desc->name));
}
return res;
}
perfctr_lock ();
scat = find_custom_category (category);
if (scat) {
char *p = custom_category_counters (scat);
int i;
res = mono_array_new (domain, mono_get_string_class (), scat->num_counters);
for (i = 0; i < scat->num_counters; ++i) {
mono_array_setref (res, i, mono_string_new (domain, p + 1));
p += 1 + strlen (p + 1) + 1; /* skip counter type and name */
p += strlen (p) + 1; /* skip counter help */
}
perfctr_unlock ();
return res;
}
perfctr_unlock ();
return mono_array_new (domain, mono_get_string_class (), 0);
}

static MonoArray*
get_string_array (void **array, int count, gboolean is_process)
{
int i;
MonoDomain *domain = mono_domain_get ();
MonoArray * res = mono_array_new (mono_domain_get (), mono_get_string_class (), count);
for (i = 0; i < count; ++i) {
char buf [128];
char *p;
if (is_process) {
char *pname = mono_process_get_name (array [i], buf, sizeof (buf));
p = g_strdup_printf ("%d/%s", GPOINTER_TO_INT (array [i]), pname);
} else {
sprintf (buf, "%d", GPOINTER_TO_INT (array [i]));
p = buf;
}
mono_array_setref (res, i, mono_string_new (domain, p));
if (p != buf)
g_free (p);
}
return res;
}

static MonoArray*
get_string_array_of_strings (void **array, int count)
{
int i;
MonoDomain *domain = mono_domain_get ();
MonoArray * res = mono_array_new (mono_domain_get (), mono_get_string_class (), count);
for (i = 0; i < count; ++i) {
char* p = array[i];
mono_array_setref (res, i, mono_string_new (domain, p));
}

return res;
}

static MonoArray*
get_mono_instances (void)
{
int count = 64;
int res;
void **buf = NULL;
MonoArray *array;
do {
count *= 2;
g_free (buf);
buf = g_new (void*, count);
res = mono_shared_area_instances (buf, count);
} while (res == count);
array = get_string_array (buf, res, TRUE);
g_free (buf);
return array;
}

static MonoArray*
get_cpu_instances (void)
{
void **buf = NULL;
int i, count;
MonoArray *array;

count = mono_cpu_count () + 1; /* +1 for "_Total" */
buf = g_new (void*, count);
for (i = 0; i < count; ++i)
buf [i] = GINT_TO_POINTER (i - 1); /* -1 => _Total */
array = get_string_array (buf, count, FALSE);
g_free (buf);
mono_array_setref (array, 0, mono_string_new (mono_domain_get (), "_Total"));
return array;
}

static MonoArray*
get_processes_instances (void)
{
MonoArray *array;
int count = 0;
void **buf = mono_process_list (&count);
if (!buf)
return get_string_array (NULL, 0, FALSE);
array = get_string_array (buf, count, TRUE);
g_free (buf);
return array;
}

static MonoArray*
get_networkinterface_instances (void)
{
MonoArray *array;
int count = 0;
void **buf = mono_networkinterface_list (&count);
if (!buf)
return get_string_array_of_strings (NULL, 0);
array = get_string_array_of_strings (buf, count);
g_strfreev ((char **) buf);
return array;
}

static MonoArray*
get_custom_instances (MonoString *category)
{
SharedCategory *scat;
scat = find_custom_category (category);
if (scat) {
GSList *list = get_custom_instances_list (scat);
GSList *tmp;
int i = 0;
MonoArray *array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
for (tmp = list; tmp; tmp = tmp->next) {
SharedInstance *inst = tmp->data;
mono_array_setref (array, i, mono_string_new (mono_domain_get (), inst->instance_name));
i++;
}
g_slist_free (list);
return array;
}
return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
}

MonoArray*
mono_perfcounter_instance_names (MonoString *category, MonoString *machine)
{
const CategoryDesc* cat;
if (mono_string_compare_ascii (machine, "."))
return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
cat = find_category (category);
if (!cat)
return get_custom_instances (category);
switch (cat->instance_type) {
case MonoInstance:
return get_mono_instances ();
case CPUInstance:
return get_cpu_instances ();
case ProcessInstance:
return get_processes_instances ();
case NetworkInterfaceInstance:
return get_networkinterface_instances ();
case ThreadInstance:
default:
return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
}
}
#else
void*
mono_perfcounter_get_impl (MonoString* category, MonoString* counter, MonoString* instance, MonoString* machine, int *type, MonoBoolean *custom)
{
g_assert_not_reached ();
}

MonoBoolean
mono_perfcounter_get_sample (void *impl, MonoBoolean only_value, MonoCounterSample *sample)
{
g_assert_not_reached ();
}

gint64
mono_perfcounter_update_value (void *impl, MonoBoolean do_incr, gint64 value)
{
g_assert_not_reached ();
}

void
mono_perfcounter_free_data (void *impl)
{
g_assert_not_reached ();
}

/* Category icalls */
MonoBoolean
mono_perfcounter_category_del (MonoString *name)
{
g_assert_not_reached ();
}

MonoString*
mono_perfcounter_category_help (MonoString *category, MonoString *machine)
{
g_assert_not_reached ();
}

MonoBoolean
mono_perfcounter_category_exists (MonoString *counter, MonoString *category, MonoString *machine)
{
g_assert_not_reached ();
}

MonoBoolean
mono_perfcounter_create (MonoString *category, MonoString *help, int type, MonoArray *items)
{
g_assert_not_reached ();
}

int
mono_perfcounter_instance_exists (MonoString *instance, MonoString *category, MonoString *machine)
{
g_assert_not_reached ();
}

MonoArray*
mono_perfcounter_category_names (MonoString *machine)
{
g_assert_not_reached ();
}

MonoArray*
mono_perfcounter_counter_names (MonoString *category, MonoString *machine)
{
g_assert_not_reached ();
}

MonoArray*
mono_perfcounter_instance_names (MonoString *category, MonoString *machine)
{
g_assert_not_reached ();
}
#endif
Something went wrong with that request. Please try again.