summaryrefslogtreecommitdiff
path: root/indra/llcommon
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llcommon')
-rw-r--r--indra/llcommon/CMakeLists.txt3
-rw-r--r--indra/llcommon/indra_constants.h2
-rw-r--r--indra/llcommon/llsys.cpp710
-rw-r--r--indra/llcommon/llsys.h22
-rw-r--r--indra/llcommon/llversionviewer.h2
-rw-r--r--indra/llcommon/tests/llsdserialize_test.cpp508
-rw-r--r--indra/llcommon/tests/setpython.py19
7 files changed, 1211 insertions, 55 deletions
diff --git a/indra/llcommon/CMakeLists.txt b/indra/llcommon/CMakeLists.txt
index 9910281b64..c755020a64 100644
--- a/indra/llcommon/CMakeLists.txt
+++ b/indra/llcommon/CMakeLists.txt
@@ -317,7 +317,8 @@ if (LL_TESTS)
LL_ADD_INTEGRATION_TEST(lllazy "" "${test_libs}")
LL_ADD_INTEGRATION_TEST(llprocessor "" "${test_libs}")
LL_ADD_INTEGRATION_TEST(llrand "" "${test_libs}")
- LL_ADD_INTEGRATION_TEST(llsdserialize "" "${test_libs}")
+ LL_ADD_INTEGRATION_TEST(llsdserialize "" "${test_libs}"
+ "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/setpython.py")
LL_ADD_INTEGRATION_TEST(llstring "" "${test_libs}")
LL_ADD_INTEGRATION_TEST(lltreeiterators "" "${test_libs}")
LL_ADD_INTEGRATION_TEST(lluri "" "${test_libs}")
diff --git a/indra/llcommon/indra_constants.h b/indra/llcommon/indra_constants.h
index d0f287657e..0745696ef3 100644
--- a/indra/llcommon/indra_constants.h
+++ b/indra/llcommon/indra_constants.h
@@ -387,8 +387,6 @@ const S32 MAP_SIM_RETURN_NULL_SIMS = 0x00010000;
const S32 MAP_SIM_PRELUDE = 0x00020000;
// Crash reporter behavior
-const char* const CRASH_SETTINGS_FILE = "settings_crash_behavior.xml";
-const char* const CRASH_BEHAVIOR_SETTING = "CrashSubmitBehavior";
const S32 CRASH_BEHAVIOR_ASK = 0;
const S32 CRASH_BEHAVIOR_ALWAYS_SEND = 1;
const S32 CRASH_BEHAVIOR_NEVER_SEND = 2;
diff --git a/indra/llcommon/llsys.cpp b/indra/llcommon/llsys.cpp
index e8616a9be6..99e61433c6 100644
--- a/indra/llcommon/llsys.cpp
+++ b/indra/llcommon/llsys.cpp
@@ -1,6 +1,6 @@
/**
* @file llsys.cpp
- * @brief Impelementation of the basic system query functions.
+ * @brief Implementation of the basic system query functions.
*
* $LicenseInfo:firstyear=2002&license=viewerlgpl$
* Second Life Viewer Source Code
@@ -24,6 +24,10 @@
* $/LicenseInfo$
*/
+#if LL_WINDOWS
+#pragma warning (disable : 4355) // 'this' used in initializer list: yes, intentionally
+#endif
+
#include "linden_common.h"
#include "llsys.h"
@@ -36,22 +40,43 @@
#endif
#include "llprocessor.h"
+#include "llerrorcontrol.h"
+#include "llevents.h"
+#include "lltimer.h"
+#include "llsdserialize.h"
+#include "llsdutil.h"
+#include <boost/bind.hpp>
+#include <boost/circular_buffer.hpp>
+#include <boost/regex.hpp>
+#include <boost/foreach.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/range.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_float.hpp>
+
+using namespace llsd;
#if LL_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# include <windows.h>
+# include <psapi.h> // GetPerformanceInfo() et al.
#elif LL_DARWIN
# include <errno.h>
# include <sys/sysctl.h>
# include <sys/utsname.h>
# include <stdint.h>
# include <Carbon/Carbon.h>
+# include <sys/wait.h>
+# include <string.h>
+# include <stdexcept>
#elif LL_LINUX
# include <errno.h>
# include <sys/utsname.h>
# include <unistd.h>
# include <sys/sysinfo.h>
+# include <stdexcept>
const char MEMINFO_FILE[] = "/proc/meminfo";
#elif LL_SOLARIS
# include <stdio.h>
@@ -70,6 +95,15 @@ extern int errno;
static const S32 CPUINFO_BUFFER_SIZE = 16383;
LLCPUInfo gSysCPU;
+// Don't log memory info any more often than this. It also serves as our
+// framerate sample size.
+static const F32 MEM_INFO_THROTTLE = 20;
+// Sliding window of samples. We intentionally limit the length of time we
+// remember "the slowest" framerate because framerate is very slow at login.
+// If we only triggered FrameWatcher logging when the session framerate
+// dropped below the login framerate, we'd have very little additional data.
+static const F32 MEM_INFO_WINDOW = 10*60;
+
#if LL_WINDOWS
#ifndef DLLVERSIONINFO
typedef struct _DllVersionInfo
@@ -613,8 +647,78 @@ void LLCPUInfo::stream(std::ostream& s) const
s << "->mCPUString: " << mCPUString << std::endl;
}
+// Helper class for LLMemoryInfo: accumulate stats in the form we store for
+// LLMemoryInfo::getStatsMap().
+class Stats
+{
+public:
+ Stats():
+ mStats(LLSD::emptyMap())
+ {}
+
+ // Store every integer type as LLSD::Integer.
+ template <class T>
+ void add(const LLSD::String& name, const T& value,
+ typename boost::enable_if<boost::is_integral<T> >::type* = 0)
+ {
+ mStats[name] = LLSD::Integer(value);
+ }
+
+ // Store every floating-point type as LLSD::Real.
+ template <class T>
+ void add(const LLSD::String& name, const T& value,
+ typename boost::enable_if<boost::is_float<T> >::type* = 0)
+ {
+ mStats[name] = LLSD::Real(value);
+ }
+
+ // Hope that LLSD::Date values are sufficiently unambiguous.
+ void add(const LLSD::String& name, const LLSD::Date& value)
+ {
+ mStats[name] = value;
+ }
+
+ LLSD get() const { return mStats; }
+
+private:
+ LLSD mStats;
+};
+
+// Wrap boost::regex_match() with a function that doesn't throw.
+template <typename S, typename M, typename R>
+static bool regex_match_no_exc(const S& string, M& match, const R& regex)
+{
+ try
+ {
+ return boost::regex_match(string, match, regex);
+ }
+ catch (const std::runtime_error& e)
+ {
+ LL_WARNS("LLMemoryInfo") << "error matching with '" << regex.str() << "': "
+ << e.what() << ":\n'" << string << "'" << LL_ENDL;
+ return false;
+ }
+}
+
+// Wrap boost::regex_search() with a function that doesn't throw.
+template <typename S, typename M, typename R>
+static bool regex_search_no_exc(const S& string, M& match, const R& regex)
+{
+ try
+ {
+ return boost::regex_search(string, match, regex);
+ }
+ catch (const std::runtime_error& e)
+ {
+ LL_WARNS("LLMemoryInfo") << "error searching with '" << regex.str() << "': "
+ << e.what() << ":\n'" << string << "'" << LL_ENDL;
+ return false;
+ }
+}
+
LLMemoryInfo::LLMemoryInfo()
{
+ refresh();
}
#if LL_WINDOWS
@@ -638,11 +742,7 @@ static U32 LLMemoryAdjustKBResult(U32 inKB)
U32 LLMemoryInfo::getPhysicalMemoryKB() const
{
#if LL_WINDOWS
- MEMORYSTATUSEX state;
- state.dwLength = sizeof(state);
- GlobalMemoryStatusEx(&state);
-
- return LLMemoryAdjustKBResult((U32)(state.ullTotalPhys >> 10));
+ return LLMemoryAdjustKBResult(mStatsMap["Total Physical KB"].asInteger());
#elif LL_DARWIN
// This might work on Linux as well. Someone check...
@@ -690,12 +790,82 @@ U32 LLMemoryInfo::getPhysicalMemoryClamped() const
void LLMemoryInfo::getAvailableMemoryKB(U32& avail_physical_mem_kb, U32& avail_virtual_mem_kb)
{
#if LL_WINDOWS
- MEMORYSTATUSEX state;
- state.dwLength = sizeof(state);
- GlobalMemoryStatusEx(&state);
+ // Sigh, this shouldn't be a static method, then we wouldn't have to
+ // reload this data separately from refresh()
+ LLSD statsMap(loadStatsMap());
+
+ avail_physical_mem_kb = statsMap["Avail Physical KB"].asInteger();
+ avail_virtual_mem_kb = statsMap["Avail Virtual KB"].asInteger();
+
+#elif LL_DARWIN
+ // mStatsMap is derived from vm_stat, look for (e.g.) "kb free":
+ // $ vm_stat
+ // Mach Virtual Memory Statistics: (page size of 4096 bytes)
+ // Pages free: 462078.
+ // Pages active: 142010.
+ // Pages inactive: 220007.
+ // Pages wired down: 159552.
+ // "Translation faults": 220825184.
+ // Pages copy-on-write: 2104153.
+ // Pages zero filled: 167034876.
+ // Pages reactivated: 65153.
+ // Pageins: 2097212.
+ // Pageouts: 41759.
+ // Object cache: 841598 hits of 7629869 lookups (11% hit rate)
+ avail_physical_mem_kb = -1 ;
+ avail_virtual_mem_kb = -1 ;
- avail_physical_mem_kb = (U32)(state.ullAvailPhys/1024) ;
- avail_virtual_mem_kb = (U32)(state.ullAvailVirtual/1024) ;
+#elif LL_LINUX
+ // mStatsMap is derived from MEMINFO_FILE:
+ // $ cat /proc/meminfo
+ // MemTotal: 4108424 kB
+ // MemFree: 1244064 kB
+ // Buffers: 85164 kB
+ // Cached: 1990264 kB
+ // SwapCached: 0 kB
+ // Active: 1176648 kB
+ // Inactive: 1427532 kB
+ // Active(anon): 529152 kB
+ // Inactive(anon): 15924 kB
+ // Active(file): 647496 kB
+ // Inactive(file): 1411608 kB
+ // Unevictable: 16 kB
+ // Mlocked: 16 kB
+ // HighTotal: 3266316 kB
+ // HighFree: 721308 kB
+ // LowTotal: 842108 kB
+ // LowFree: 522756 kB
+ // SwapTotal: 6384632 kB
+ // SwapFree: 6384632 kB
+ // Dirty: 28 kB
+ // Writeback: 0 kB
+ // AnonPages: 528820 kB
+ // Mapped: 89472 kB
+ // Shmem: 16324 kB
+ // Slab: 159624 kB
+ // SReclaimable: 145168 kB
+ // SUnreclaim: 14456 kB
+ // KernelStack: 2560 kB
+ // PageTables: 5560 kB
+ // NFS_Unstable: 0 kB
+ // Bounce: 0 kB
+ // WritebackTmp: 0 kB
+ // CommitLimit: 8438844 kB
+ // Committed_AS: 1271596 kB
+ // VmallocTotal: 122880 kB
+ // VmallocUsed: 65252 kB
+ // VmallocChunk: 52356 kB
+ // HardwareCorrupted: 0 kB
+ // HugePages_Total: 0
+ // HugePages_Free: 0
+ // HugePages_Rsvd: 0
+ // HugePages_Surp: 0
+ // Hugepagesize: 2048 kB
+ // DirectMap4k: 434168 kB
+ // DirectMap2M: 477184 kB
+ // (could also run 'free', but easier to read a file than run a program)
+ avail_physical_mem_kb = -1 ;
+ avail_virtual_mem_kb = -1 ;
#else
//do not know how to collect available memory info for other systems.
@@ -708,56 +878,389 @@ void LLMemoryInfo::getAvailableMemoryKB(U32& avail_physical_mem_kb, U32& avail_v
void LLMemoryInfo::stream(std::ostream& s) const
{
+ // We want these memory stats to be easy to grep from the log, along with
+ // the timestamp. So preface each line with the timestamp and a
+ // distinctive marker. Without that, we'd have to search the log for the
+ // introducer line, then read subsequent lines, etc...
+ std::string pfx(LLError::utcTime() + " <mem> ");
+
+ // Max key length
+ size_t key_width(0);
+ BOOST_FOREACH(const MapEntry& pair, inMap(mStatsMap))
+ {
+ size_t len(pair.first.length());
+ if (len > key_width)
+ {
+ key_width = len;
+ }
+ }
+
+ // Now stream stats
+ BOOST_FOREACH(const MapEntry& pair, inMap(mStatsMap))
+ {
+ s << pfx << std::setw(key_width+1) << (pair.first + ':') << ' ';
+ LLSD value(pair.second);
+ if (value.isInteger())
+ s << std::setw(12) << value.asInteger();
+ else if (value.isReal())
+ s << std::fixed << std::setprecision(1) << value.asReal();
+ else if (value.isDate())
+ value.asDate().toStream(s);
+ else
+ s << value; // just use default LLSD formatting
+ s << std::endl;
+ }
+}
+
+LLSD LLMemoryInfo::getStatsMap() const
+{
+ return mStatsMap;
+}
+
+LLMemoryInfo& LLMemoryInfo::refresh()
+{
+ mStatsMap = loadStatsMap();
+
+ LL_DEBUGS("LLMemoryInfo") << "Populated mStatsMap:\n";
+ LLSDSerialize::toPrettyXML(mStatsMap, LL_CONT);
+ LL_ENDL;
+
+ return *this;
+}
+
+LLSD LLMemoryInfo::loadStatsMap()
+{
+ // This implementation is derived from stream() code (as of 2011-06-29).
+ Stats stats;
+
+ // associate timestamp for analysis over time
+ stats.add("timestamp", LLDate::now());
+
#if LL_WINDOWS
MEMORYSTATUSEX state;
state.dwLength = sizeof(state);
GlobalMemoryStatusEx(&state);
- s << "Percent Memory use: " << (U32)state.dwMemoryLoad << '%' << std::endl;
- s << "Total Physical KB: " << (U32)(state.ullTotalPhys/1024) << std::endl;
- s << "Avail Physical KB: " << (U32)(state.ullAvailPhys/1024) << std::endl;
- s << "Total page KB: " << (U32)(state.ullTotalPageFile/1024) << std::endl;
- s << "Avail page KB: " << (U32)(state.ullAvailPageFile/1024) << std::endl;
- s << "Total Virtual KB: " << (U32)(state.ullTotalVirtual/1024) << std::endl;
- s << "Avail Virtual KB: " << (U32)(state.ullAvailVirtual/1024) << std::endl;
+ stats.add("Percent Memory use", state.dwMemoryLoad);
+ stats.add("Total Physical KB", state.ullTotalPhys/1024);
+ stats.add("Avail Physical KB", state.ullAvailPhys/1024);
+ stats.add("Total page KB", state.ullTotalPageFile/1024);
+ stats.add("Avail page KB", state.ullAvailPageFile/1024);
+ stats.add("Total Virtual KB", state.ullTotalVirtual/1024);
+ stats.add("Avail Virtual KB", state.ullAvailVirtual/1024);
+
+ PERFORMANCE_INFORMATION perf;
+ perf.cb = sizeof(perf);
+ GetPerformanceInfo(&perf, sizeof(perf));
+
+ SIZE_T pagekb(perf.PageSize/1024);
+ stats.add("CommitTotal KB", perf.CommitTotal * pagekb);
+ stats.add("CommitLimit KB", perf.CommitLimit * pagekb);
+ stats.add("CommitPeak KB", perf.CommitPeak * pagekb);
+ stats.add("PhysicalTotal KB", perf.PhysicalTotal * pagekb);
+ stats.add("PhysicalAvail KB", perf.PhysicalAvailable * pagekb);
+ stats.add("SystemCache KB", perf.SystemCache * pagekb);
+ stats.add("KernelTotal KB", perf.KernelTotal * pagekb);
+ stats.add("KernelPaged KB", perf.KernelPaged * pagekb);
+ stats.add("KernelNonpaged KB", perf.KernelNonpaged * pagekb);
+ stats.add("PageSize KB", pagekb);
+ stats.add("HandleCount", perf.HandleCount);
+ stats.add("ProcessCount", perf.ProcessCount);
+ stats.add("ThreadCount", perf.ThreadCount);
+
+ PROCESS_MEMORY_COUNTERS_EX pmem;
+ pmem.cb = sizeof(pmem);
+ // GetProcessMemoryInfo() is documented to accept either
+ // PROCESS_MEMORY_COUNTERS* or PROCESS_MEMORY_COUNTERS_EX*, presumably
+ // using the redundant size info to distinguish. But its prototype
+ // specifically accepts PROCESS_MEMORY_COUNTERS*, and since this is a
+ // classic-C API, PROCESS_MEMORY_COUNTERS_EX isn't a subclass. Cast the
+ // pointer.
+ GetProcessMemoryInfo(GetCurrentProcess(), PPROCESS_MEMORY_COUNTERS(&pmem), sizeof(pmem));
+
+ stats.add("Page Fault Count", pmem.PageFaultCount);
+ stats.add("PeakWorkingSetSize KB", pmem.PeakWorkingSetSize/1024);
+ stats.add("WorkingSetSize KB", pmem.WorkingSetSize/1024);
+ stats.add("QutaPeakPagedPoolUsage KB", pmem.QuotaPeakPagedPoolUsage/1024);
+ stats.add("QuotaPagedPoolUsage KB", pmem.QuotaPagedPoolUsage/1024);
+ stats.add("QuotaPeakNonPagedPoolUsage KB", pmem.QuotaPeakNonPagedPoolUsage/1024);
+ stats.add("QuotaNonPagedPoolUsage KB", pmem.QuotaNonPagedPoolUsage/1024);
+ stats.add("PagefileUsage KB", pmem.PagefileUsage/1024);
+ stats.add("PeakPagefileUsage KB", pmem.PeakPagefileUsage/1024);
+ stats.add("PrivateUsage KB", pmem.PrivateUsage/1024);
+
#elif LL_DARWIN
uint64_t phys = 0;
size_t len = sizeof(phys);
- if(sysctlbyname("hw.memsize", &phys, &len, NULL, 0) == 0)
+ if (sysctlbyname("hw.memsize", &phys, &len, NULL, 0) == 0)
{
- s << "Total Physical KB: " << phys/1024 << std::endl;
+ stats.add("Total Physical KB", phys/1024);
}
else
{
- s << "Unable to collect memory information";
+ LL_WARNS("LLMemoryInfo") << "Unable to collect hw.memsize memory information" << LL_ENDL;
+ }
+
+ FILE* pout = popen("vm_stat 2>&1", "r");
+ if (! pout) // popen() couldn't run vm_stat
+ {
+ // Save errno right away.
+ int popen_errno(errno);
+ LL_WARNS("LLMemoryInfo") << "Unable to collect vm_stat memory information: ";
+ char buffer[256];
+ if (0 == strerror_r(popen_errno, buffer, sizeof(buffer)))
+ {
+ LL_CONT << buffer;
+ }
+ else
+ {
+ LL_CONT << "errno " << popen_errno;
+ }
+ LL_CONT << LL_ENDL;
+ }
+ else // popen() launched vm_stat
+ {
+ // Mach Virtual Memory Statistics: (page size of 4096 bytes)
+ // Pages free: 462078.
+ // Pages active: 142010.
+ // Pages inactive: 220007.
+ // Pages wired down: 159552.
+ // "Translation faults": 220825184.
+ // Pages copy-on-write: 2104153.
+ // Pages zero filled: 167034876.
+ // Pages reactivated: 65153.
+ // Pageins: 2097212.
+ // Pageouts: 41759.
+ // Object cache: 841598 hits of 7629869 lookups (11% hit rate)
+
+ // Intentionally don't pass the boost::no_except flag. These
+ // boost::regex objects are constructed with string literals, so they
+ // should be valid every time. If they become invalid, we WANT an
+ // exception, hopefully even before the dev checks in.
+ boost::regex pagesize_rx("\\(page size of ([0-9]+) bytes\\)");
+ boost::regex stat_rx("(.+): +([0-9]+)\\.");
+ boost::regex cache_rx("Object cache: ([0-9]+) hits of ([0-9]+) lookups "
+ "\\(([0-9]+)% hit rate\\)");
+ boost::cmatch matched;
+ LLSD::Integer pagesizekb(4096/1024);
+
+ // Here 'pout' is vm_stat's stdout. Search it for relevant data.
+ char line[100];
+ line[sizeof(line)-1] = '\0';
+ while (fgets(line, sizeof(line)-1, pout))
+ {
+ size_t linelen(strlen(line));
+ // Truncate any trailing newline
+ if (line[linelen - 1] == '\n')
+ {
+ line[--linelen] = '\0';
+ }
+ LL_DEBUGS("LLMemoryInfo") << line << LL_ENDL;
+ if (regex_search_no_exc(line, matched, pagesize_rx))
+ {
+ // "Mach Virtual Memory Statistics: (page size of 4096 bytes)"
+ std::string pagesize_str(matched[1].first, matched[1].second);
+ try
+ {
+ // Reasonable to assume that pagesize will always be a
+ // multiple of 1Kb?
+ pagesizekb = boost::lexical_cast<LLSD::Integer>(pagesize_str)/1024;
+ }
+ catch (const boost::bad_lexical_cast&)
+ {
+ LL_WARNS("LLMemoryInfo") << "couldn't parse '" << pagesize_str
+ << "' in vm_stat line: " << line << LL_ENDL;
+ continue;
+ }
+ stats.add("page size", pagesizekb);
+ }
+ else if (regex_match_no_exc(line, matched, stat_rx))
+ {
+ // e.g. "Pages free: 462078."
+ // Strip double-quotes off certain statistic names
+ const char *key_begin(matched[1].first), *key_end(matched[1].second);
+ if (key_begin[0] == '"' && key_end[-1] == '"')
+ {
+ ++key_begin;
+ --key_end;
+ }
+ LLSD::String key(key_begin, key_end);
+ LLSD::String value_str(matched[2].first, matched[2].second);
+ LLSD::Integer value(0);
+ try
+ {
+ value = boost::lexical_cast<LLSD::Integer>(value_str);
+ }
+ catch (const boost::bad_lexical_cast&)
+ {
+ LL_WARNS("LLMemoryInfo") << "couldn't parse '" << value_str
+ << "' in vm_stat line: " << line << LL_ENDL;
+ continue;
+ }
+ // Store this statistic.
+ stats.add(key, value);
+ // Is this in units of pages? If so, convert to Kb.
+ static const LLSD::String pages("Pages ");
+ if (key.substr(0, pages.length()) == pages)
+ {
+ // Synthesize a new key with kb in place of Pages
+ LLSD::String kbkey("kb ");
+ kbkey.append(key.substr(pages.length()));
+ stats.add(kbkey, value * pagesizekb);
+ }
+ }
+ else if (regex_match_no_exc(line, matched, cache_rx))
+ {
+ // e.g. "Object cache: 841598 hits of 7629869 lookups (11% hit rate)"
+ static const char* cache_keys[] = { "cache hits", "cache lookups", "cache hit%" };
+ std::vector<LLSD::Integer> cache_values;
+ for (size_t i = 0; i < (sizeof(cache_keys)/sizeof(cache_keys[0])); ++i)
+ {
+ LLSD::String value_str(matched[i+1].first, matched[i+1].second);
+ LLSD::Integer value(0);
+ try
+ {
+ value = boost::lexical_cast<LLSD::Integer>(value_str);
+ }
+ catch (boost::bad_lexical_cast&)
+ {
+ LL_WARNS("LLMemoryInfo") << "couldn't parse '" << value_str
+ << "' in vm_stat line: " << line << LL_ENDL;
+ continue;
+ }
+ stats.add(cache_keys[i], value);
+ }
+ }
+ else
+ {
+ LL_WARNS("LLMemoryInfo") << "unrecognized vm_stat line: " << line << LL_ENDL;
+ }
+ }
+ int status(pclose(pout));
+ if (status == -1) // pclose() couldn't retrieve rc
+ {
+ // Save errno right away.
+ int pclose_errno(errno);
+ // The ECHILD error happens so frequently that unless filtered,
+ // the warning below spams the log file. This is too bad, because
+ // sometimes the logic above fails to produce any output derived
+ // from vm_stat, but we've been unable to observe any specific
+ // error indicating the problem.
+ if (pclose_errno != ECHILD)
+ {
+ LL_WARNS("LLMemoryInfo") << "Unable to obtain vm_stat termination code: ";
+ char buffer[256];
+ if (0 == strerror_r(pclose_errno, buffer, sizeof(buffer)))
+ {
+ LL_CONT << buffer;
+ }
+ else
+ {
+ LL_CONT << "errno " << pclose_errno;
+ }
+ LL_CONT << LL_ENDL;
+ }
+ }
+ else // pclose() retrieved rc; analyze
+ {
+ if (WIFEXITED(status))
+ {
+ int rc(WEXITSTATUS(status));
+ if (rc != 0)
+ {
+ LL_WARNS("LLMemoryInfo") << "vm_stat terminated with rc " << rc << LL_ENDL;
+ }
+ }
+ else if (WIFSIGNALED(status))
+ {
+ LL_WARNS("LLMemoryInfo") << "vm_stat terminated by signal " << WTERMSIG(status)
+ << LL_ENDL;
+ }
+ }
}
+
#elif LL_SOLARIS
- U64 phys = 0;
+ U64 phys = 0;
- phys = (U64)(sysconf(_SC_PHYS_PAGES)) * (U64)(sysconf(_SC_PAGESIZE)/1024);
+ phys = (U64)(sysconf(_SC_PHYS_PAGES)) * (U64)(sysconf(_SC_PAGESIZE)/1024);
- s << "Total Physical KB: " << phys << std::endl;
-#else
- // *NOTE: This works on linux. What will it do on other systems?
- LLFILE* meminfo = LLFile::fopen(MEMINFO_FILE,"rb");
- if(meminfo)
+ stats.add("Total Physical KB", phys);
+
+#elif LL_LINUX
+ std::ifstream meminfo(MEMINFO_FILE);
+ if (meminfo.is_open())
{
- char line[MAX_STRING]; /* Flawfinder: ignore */
- memset(line, 0, MAX_STRING);
- while(fgets(line, MAX_STRING, meminfo))
+ // MemTotal: 4108424 kB
+ // MemFree: 1244064 kB
+ // Buffers: 85164 kB
+ // Cached: 1990264 kB
+ // SwapCached: 0 kB
+ // Active: 1176648 kB
+ // Inactive: 1427532 kB
+ // ...
+ // VmallocTotal: 122880 kB
+ // VmallocUsed: 65252 kB
+ // VmallocChunk: 52356 kB
+ // HardwareCorrupted: 0 kB
+ // HugePages_Total: 0
+ // HugePages_Free: 0
+ // HugePages_Rsvd: 0
+ // HugePages_Surp: 0
+ // Hugepagesize: 2048 kB
+ // DirectMap4k: 434168 kB
+ // DirectMap2M: 477184 kB
+
+ // Intentionally don't pass the boost::no_except flag. This
+ // boost::regex object is constructed with a string literal, so it
+ // should be valid every time. If it becomes invalid, we WANT an
+ // exception, hopefully even before the dev checks in.
+ boost::regex stat_rx("(.+): +([0-9]+)( kB)?");
+ boost::smatch matched;
+
+ std::string line;
+ while (std::getline(meminfo, line))
{
- line[strlen(line)-1] = ' '; /*Flawfinder: ignore*/
- s << line;
+ LL_DEBUGS("LLMemoryInfo") << line << LL_ENDL;
+ if (regex_match_no_exc(line, matched, stat_rx))
+ {
+ // e.g. "MemTotal: 4108424 kB"
+ LLSD::String key(matched[1].first, matched[1].second);
+ LLSD::String value_str(matched[2].first, matched[2].second);
+ LLSD::Integer value(0);
+ try
+ {
+ value = boost::lexical_cast<LLSD::Integer>(value_str);
+ }
+ catch (const boost::bad_lexical_cast&)
+ {
+ LL_WARNS("LLMemoryInfo") << "couldn't parse '" << value_str
+ << "' in " << MEMINFO_FILE << " line: "
+ << line << LL_ENDL;
+ continue;
+ }
+ // Store this statistic.
+ stats.add(key, value);
+ }
+ else
+ {
+ LL_WARNS("LLMemoryInfo") << "unrecognized " << MEMINFO_FILE << " line: "
+ << line << LL_ENDL;
+ }
}
- fclose(meminfo);
}
else
{
- s << "Unable to collect memory information";
+ LL_WARNS("LLMemoryInfo") << "Unable to collect memory information" << LL_ENDL;
}
+
+#else
+ LL_WARNS("LLMemoryInfo") << "Unknown system; unable to collect memory information" << LL_ENDL;
+
#endif
+
+ return stats.get();
}
std::ostream& operator<<(std::ostream& s, const LLOSInfo& info)
@@ -778,6 +1281,143 @@ std::ostream& operator<<(std::ostream& s, const LLMemoryInfo& info)
return s;
}
+class FrameWatcher
+{
+public:
+ FrameWatcher():
+ // Hooking onto the "mainloop" event pump gets us one call per frame.
+ mConnection(LLEventPumps::instance()
+ .obtain("mainloop")
+ .listen("FrameWatcher", boost::bind(&FrameWatcher::tick, this, _1))),
+ // Initializing mSampleStart to an invalid timestamp alerts us to skip
+ // trying to compute framerate on the first call.
+ mSampleStart(-1),
+ // Initializing mSampleEnd to 0 ensures that we treat the first call
+ // as the completion of a sample window.
+ mSampleEnd(0),
+ mFrames(0),
+ // Both MEM_INFO_WINDOW and MEM_INFO_THROTTLE are in seconds. We need
+ // the number of integer MEM_INFO_THROTTLE sample slots that will fit
+ // in MEM_INFO_WINDOW. Round up.
+ mSamples(int((MEM_INFO_WINDOW / MEM_INFO_THROTTLE) + 0.7)),
+ // Initializing to F32_MAX means that the first real frame will become
+ // the slowest ever, which sounds like a good idea.
+ mSlowest(F32_MAX)
+ {}
+
+ bool tick(const LLSD&)
+ {
+ F32 timestamp(mTimer.getElapsedTimeF32());
+
+ // Count this frame in the interval just completed.
+ ++mFrames;
+
+ // Have we finished a sample window yet?
+ if (timestamp < mSampleEnd)
+ {
+ // no, just keep waiting
+ return false;
+ }
+
+ // Set up for next sample window. Capture values for previous frame in
+ // local variables and reset data members.
+ U32 frames(mFrames);
+ F32 sampleStart(mSampleStart);
+ // No frames yet in next window
+ mFrames = 0;
+ // which starts right now
+ mSampleStart = timestamp;
+ // and ends MEM_INFO_THROTTLE seconds in the future
+ mSampleEnd = mSampleStart + MEM_INFO_THROTTLE;
+
+ // On the very first call, that's all we can do, no framerate
+ // computation is possible.
+ if (sampleStart < 0)
+ {
+ return false;
+ }
+
+ // How long did this actually take? As framerate slows, the duration
+ // of the frame we just finished could push us WELL beyond our desired
+ // sample window size.
+ F32 elapsed(timestamp - sampleStart);
+ F32 framerate(frames/elapsed);
+
+ // Remember previous slowest framerate because we're just about to
+ // update it.
+ F32 slowest(mSlowest);
+ // Remember previous number of samples.
+ boost::circular_buffer<F32>::size_type prevSize(mSamples.size());
+
+ // Capture new framerate in our samples buffer. Once the buffer is
+ // full (after MEM_INFO_WINDOW seconds), this will displace the oldest
+ // sample. ("So they all rolled over, and one fell out...")
+ mSamples.push_back(framerate);
+
+ // Calculate the new minimum framerate. I know of no way to update a
+ // rolling minimum without ever rescanning the buffer. But since there
+ // are only a few tens of items in this buffer, rescanning it is
+ // probably cheaper (and certainly easier to reason about) than
+ // attempting to optimize away some of the scans.
+ mSlowest = framerate; // pick an arbitrary entry to start
+ for (boost::circular_buffer<F32>::const_iterator si(mSamples.begin()), send(mSamples.end());
+ si != send; ++si)
+ {
+ if (*si < mSlowest)
+ {
+ mSlowest = *si;
+ }
+ }
+
+ // We're especially interested in memory as framerate drops. Only log
+ // when framerate drops below the slowest framerate we remember.
+ // (Should always be true for the end of the very first sample
+ // window.)
+ if (framerate >= slowest)
+ {
+ return false;
+ }
+ // Congratulations, we've hit a new low. :-P
+
+ LL_INFOS("FrameWatcher") << ' ';
+ if (! prevSize)
+ {
+ LL_CONT << "initial framerate ";
+ }
+ else
+ {
+ LL_CONT << "slowest framerate for last " << int(prevSize * MEM_INFO_THROTTLE)
+ << " seconds ";
+ }
+ LL_CONT << std::fixed << std::setprecision(1) << framerate << '\n'
+ << LLMemoryInfo() << LL_ENDL;
+
+ return false;
+ }
+
+private:
+ // Storing the connection in an LLTempBoundListener ensures it will be
+ // disconnected when we're destroyed.
+ LLTempBoundListener mConnection;
+ // Track elapsed time
+ LLTimer mTimer;
+ // Some of what you see here is in fact redundant with functionality you
+ // can get from LLTimer. Unfortunately the LLTimer API is missing the
+ // feature we need: has at least the stated interval elapsed, and if so,
+ // exactly how long has passed? So we have to do it by hand, sigh.
+ // Time at start, end of sample window
+ F32 mSampleStart, mSampleEnd;
+ // Frames this sample window
+ U32 mFrames;
+ // Sliding window of framerate samples
+ boost::circular_buffer<F32> mSamples;
+ // Slowest framerate in mSamples
+ F32 mSlowest;
+};
+
+// Need an instance of FrameWatcher before it does any good
+static FrameWatcher sFrameWatcher;
+
BOOL gunzip_file(const std::string& srcfile, const std::string& dstfile)
{
std::string tmpfile;
diff --git a/indra/llcommon/llsys.h b/indra/llcommon/llsys.h
index 41a4f25000..739e795d3a 100644
--- a/indra/llcommon/llsys.h
+++ b/indra/llcommon/llsys.h
@@ -36,6 +36,7 @@
// llinfos << info << llendl;
//
+#include "llsd.h"
#include <iosfwd>
#include <string>
@@ -117,6 +118,27 @@ public:
//get the available memory infomation in KiloBytes.
static void getAvailableMemoryKB(U32& avail_physical_mem_kb, U32& avail_virtual_mem_kb);
+
+ // Retrieve a map of memory statistics. The keys of the map are platform-
+ // dependent. The values are in kilobytes to try to avoid integer overflow.
+ LLSD getStatsMap() const;
+
+ // Re-fetch memory data (as reported by stream() and getStatsMap()) from the
+ // system. Normally this is fetched at construction time. Return (*this)
+ // to permit usage of the form:
+ // @code
+ // LLMemoryInfo info;
+ // ...
+ // info.refresh().getStatsMap();
+ // @endcode
+ LLMemoryInfo& refresh();
+
+private:
+ // set mStatsMap
+ static LLSD loadStatsMap();
+
+ // Memory stats for getStatsMap().
+ LLSD mStatsMap;
};
diff --git a/indra/llcommon/llversionviewer.h b/indra/llcommon/llversionviewer.h
index 0018b8e844..6c1d233425 100644
--- a/indra/llcommon/llversionviewer.h
+++ b/indra/llcommon/llversionviewer.h
@@ -29,7 +29,7 @@
const S32 LL_VERSION_MAJOR = 2;
const S32 LL_VERSION_MINOR = 8;
-const S32 LL_VERSION_PATCH = 1;
+const S32 LL_VERSION_PATCH = 2;
const S32 LL_VERSION_BUILD = 0;
const char * const LL_CHANNEL = "Second Life Developer";
diff --git a/indra/llcommon/tests/llsdserialize_test.cpp b/indra/llcommon/tests/llsdserialize_test.cpp
index 7b4c7d6a48..72322c3b72 100644
--- a/indra/llcommon/tests/llsdserialize_test.cpp
+++ b/indra/llcommon/tests/llsdserialize_test.cpp
@@ -25,35 +25,293 @@
* $/LicenseInfo$
*/
-#if !LL_WINDOWS
+
+#include "linden_common.h"
+
+#if LL_WINDOWS
+#include <winsock2.h>
+typedef U32 uint32_t;
+#include <process.h>
+#include <io.h>
+#else
+#include <unistd.h>
#include <netinet/in.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include "llprocesslauncher.h"
#endif
-#include "linden_common.h"
+#include <sstream>
+
+/*==========================================================================*|
+// Whoops, seems Linden's Boost package and the viewer are built with
+// different settings of VC's /Zc:wchar_t switch! Using Boost.Filesystem
+// pathname operations produces Windows link errors:
+// unresolved external symbol "private: static class std::codecvt<unsigned short,
+// char,int> const * & __cdecl boost::filesystem3::path::wchar_t_codecvt_facet()"
+// unresolved external symbol "void __cdecl boost::filesystem3::path_traits::convert()"
+// See:
+// http://boost.2283326.n4.nabble.com/filesystem-v3-unicode-and-std-codecvt-linker-error-td3455549.html
+// which points to:
+// http://msdn.microsoft.com/en-us/library/dh8che7s%28v=VS.100%29.aspx
+
+// As we're not trying to preserve compatibility with old Boost.Filesystem
+// code, but rather writing brand-new code, use the newest available
+// Filesystem API.
+#define BOOST_FILESYSTEM_VERSION 3
+#include "boost/filesystem.hpp"
+#include "boost/filesystem/v3/fstream.hpp"
+|*==========================================================================*/
+#include "boost/range.hpp"
+#include "boost/foreach.hpp"
+#include "boost/function.hpp"
+#include "boost/lambda/lambda.hpp"
+#include "boost/lambda/bind.hpp"
+namespace lambda = boost::lambda;
+/*==========================================================================*|
+// Aaaarrgh, Linden's Boost package doesn't even include Boost.Iostreams!
+#include "boost/iostreams/stream.hpp"
+#include "boost/iostreams/device/file_descriptor.hpp"
+|*==========================================================================*/
+
#include "../llsd.h"
#include "../llsdserialize.h"
+#include "llsdutil.h"
#include "../llformat.h"
#include "../test/lltut.h"
+#include "stringize.h"
+std::vector<U8> string_to_vector(const std::string& str)
+{
+ return std::vector<U8>(str.begin(), str.end());
+}
-#if LL_WINDOWS
-#include <winsock2.h>
-typedef U32 uint32_t;
-#endif
+#if ! LL_WINDOWS
+// We want to call strerror_r(), but alarmingly, there are two different
+// variants. The one that returns int always populates the passed buffer
+// (except in case of error), whereas the other one always returns a valid
+// char* but might or might not populate the passed buffer. How do we know
+// which one we're getting? Define adapters for each and let the compiler
+// select the applicable adapter.
-std::vector<U8> string_to_vector(std::string str)
+// strerror_r() returns char*
+std::string message_from(int /*orig_errno*/, const char* /*buffer*/, const char* strerror_ret)
{
- // bc LLSD can't...
- size_t len = (size_t)str.length();
- std::vector<U8> v(len);
- for (size_t i = 0; i < len ; i++)
- {
- v[i] = str[i];
- }
- return v;
+ return strerror_ret;
}
+// strerror_r() returns int
+std::string message_from(int orig_errno, const char* buffer, int strerror_ret)
+{
+ if (strerror_ret == 0)
+ {
+ return buffer;
+ }
+ // Here strerror_r() has set errno. Since strerror_r() has already failed,
+ // seems like a poor bet to call it again to diagnose its own error...
+ int stre_errno = errno;
+ if (stre_errno == ERANGE)
+ {
+ return STRINGIZE("strerror_r() can't explain errno " << orig_errno
+ << " (buffer too small)");
+ }
+ if (stre_errno == EINVAL)
+ {
+ return STRINGIZE("unknown errno " << orig_errno);
+ }
+ // Here we don't even understand the errno from strerror_r()!
+ return STRINGIZE("strerror_r() can't explain errno " << orig_errno
+ << " (error " << stre_errno << ')');
+}
+#endif // ! LL_WINDOWS
+
+// boost::filesystem::temp_directory_path() isn't yet in Boost 1.45! :-(
+std::string temp_directory_path()
+{
+#if LL_WINDOWS
+ char buffer[4096];
+ GetTempPathA(sizeof(buffer), buffer);
+ return buffer;
+
+#else // LL_DARWIN, LL_LINUX
+ static const char* vars[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR" };
+ BOOST_FOREACH(const char* var, vars)
+ {
+ const char* found = getenv(var);
+ if (found)
+ return found;
+ }
+ return "/tmp";
+#endif // LL_DARWIN, LL_LINUX
+}
+
+// Windows presents a kinda sorta compatibility layer. Code to the yucky
+// Windows names because they're less likely than the Posix names to collide
+// with any other names in this source.
+#if LL_WINDOWS
+#define _remove DeleteFileA
+#else // ! LL_WINDOWS
+#define _open open
+#define _write write
+#define _close close
+#define _remove remove
+#endif // ! LL_WINDOWS
+
+// Create a text file with specified content "somewhere in the
+// filesystem," cleaning up when it goes out of scope.
+class NamedTempFile
+{
+public:
+ // Function that accepts an ostream ref and (presumably) writes stuff to
+ // it, e.g.:
+ // (lambda::_1 << "the value is " << 17 << '\n')
+ typedef boost::function<void(std::ostream&)> Streamer;
+
+ NamedTempFile(const std::string& ext, const std::string& content):
+ mPath(temp_directory_path())
+ {
+ createFile(ext, lambda::_1 << content);
+ }
+
+ // Disambiguate when passing string literal
+ NamedTempFile(const std::string& ext, const char* content):
+ mPath(temp_directory_path())
+ {
+ createFile(ext, lambda::_1 << content);
+ }
+
+ NamedTempFile(const std::string& ext, const Streamer& func):
+ mPath(temp_directory_path())
+ {
+ createFile(ext, func);
+ }
+
+ ~NamedTempFile()
+ {
+ _remove(mPath.c_str());
+ }
+
+ std::string getName() const { return mPath; }
+
+private:
+ void createFile(const std::string& ext, const Streamer& func)
+ {
+ // Silly maybe, but use 'ext' as the name prefix. Strip off a leading
+ // '.' if present.
+ int pfx_offset = ((! ext.empty()) && ext[0] == '.')? 1 : 0;
+
+#if ! LL_WINDOWS
+ // Make sure mPath ends with a directory separator, if it doesn't already.
+ if (mPath.empty() ||
+ ! (mPath[mPath.length() - 1] == '\\' || mPath[mPath.length() - 1] == '/'))
+ {
+ mPath.append("/");
+ }
+
+ // mkstemp() accepts and modifies a char* template string. Generate
+ // the template string, then copy to modifiable storage.
+ // mkstemp() requires its template string to end in six X's.
+ mPath += ext.substr(pfx_offset) + "XXXXXX";
+ // Copy to vector<char>
+ std::vector<char> pathtemplate(mPath.begin(), mPath.end());
+ // append a nul byte for classic-C semantics
+ pathtemplate.push_back('\0');
+ // std::vector promises that a pointer to the 0th element is the same
+ // as a pointer to a contiguous classic-C array
+ int fd(mkstemp(&pathtemplate[0]));
+ if (fd == -1)
+ {
+ // The documented errno values (http://linux.die.net/man/3/mkstemp)
+ // are used in a somewhat unusual way, so provide context-specific
+ // errors.
+ if (errno == EEXIST)
+ {
+ LL_ERRS("NamedTempFile") << "mkstemp(\"" << mPath
+ << "\") could not create unique file " << LL_ENDL;
+ }
+ if (errno == EINVAL)
+ {
+ LL_ERRS("NamedTempFile") << "bad mkstemp() file path template '"
+ << mPath << "'" << LL_ENDL;
+ }
+ // Shrug, something else
+ int mkst_errno = errno;
+ char buffer[256];
+ LL_ERRS("NamedTempFile") << "mkstemp(\"" << mPath << "\") failed: "
+ << message_from(mkst_errno, buffer,
+ strerror_r(mkst_errno, buffer, sizeof(buffer)))
+ << LL_ENDL;
+ }
+ // mkstemp() seems to have worked! Capture the modified filename.
+ // Avoid the nul byte we appended.
+ mPath.assign(pathtemplate.begin(), (pathtemplate.end()-1));
+
+/*==========================================================================*|
+ // Define an ostream on the open fd. Tell it to close fd on destruction.
+ boost::iostreams::stream<boost::iostreams::file_descriptor_sink>
+ out(fd, boost::iostreams::close_handle);
+|*==========================================================================*/
+
+ // Write desired content.
+ std::ostringstream out;
+ // Stream stuff to it.
+ func(out);
+
+ std::string data(out.str());
+ int written(_write(fd, data.c_str(), data.length()));
+ int closed(_close(fd));
+ llassert_always(written == data.length() && closed == 0);
+
+#else // LL_WINDOWS
+ // GetTempFileName() is documented to require a MAX_PATH buffer.
+ char tempname[MAX_PATH];
+ // Use 'ext' as filename prefix, but skip leading '.' if any.
+ // The 0 param is very important: requests iterating until we get a
+ // unique name.
+ if (0 == GetTempFileNameA(mPath.c_str(), ext.c_str() + pfx_offset, 0, tempname))
+ {
+ // I always have to look up this call... :-P
+ LPSTR msgptr;
+ FormatMessageA(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ LPSTR(&msgptr), // have to cast (char**) to (char*)
+ 0, NULL );
+ LL_ERRS("NamedTempFile") << "GetTempFileName(\"" << mPath << "\", \""
+ << (ext.c_str() + pfx_offset) << "\") failed: "
+ << msgptr << LL_ENDL;
+ LocalFree(msgptr);
+ }
+ // GetTempFileName() appears to have worked! Capture the actual
+ // filename.
+ mPath = tempname;
+ // Open the file and stream content to it. Destructor will close.
+ std::ofstream out(tempname);
+ func(out);
+
+#endif // LL_WINDOWS
+ }
+
+ void peep()
+ {
+ std::cout << "File '" << mPath << "' contains:\n";
+ std::ifstream reader(mPath.c_str());
+ std::string line;
+ while (std::getline(reader, line))
+ std::cout << line << '\n';
+ std::cout << "---\n";
+ }
+
+ std::string mPath;
+};
+
namespace tut
{
struct sd_xml_data
@@ -1494,5 +1752,223 @@ namespace tut
ensureBinaryAndNotation("map", test);
ensureBinaryAndXML("map", test);
}
-}
+ struct TestPythonCompatible
+ {
+ TestPythonCompatible():
+ // Note the peculiar insertion of __FILE__ into this string. Since
+ // this script is being written into a platform-dependent temp
+ // directory, we can't locate indra/lib/python relative to
+ // Python's __file__. Use __FILE__ instead, navigating relative
+ // to this C++ source file. Use Python raw-string syntax so
+ // Windows pathname backslashes won't mislead Python's string
+ // scanner.
+ import_llsd("import os.path\n"
+ "import sys\n"
+ "sys.path.insert(0,\n"
+ " os.path.join(os.path.dirname(r'" __FILE__ "'),\n"
+ " os.pardir, os.pardir, 'lib', 'python'))\n"
+ "try:\n"
+ " from llbase import llsd\n"
+ "except ImportError:\n"
+ " from indra.base import llsd\n")
+ {}
+ ~TestPythonCompatible() {}
+
+ std::string import_llsd;
+
+ template <typename CONTENT>
+ void python(const std::string& desc, const CONTENT& script, int expect=0)
+ {
+ const char* PYTHON(getenv("PYTHON"));
+ ensure("Set $PYTHON to the Python interpreter", PYTHON);
+
+ NamedTempFile scriptfile(".py", script);
+
+#if LL_WINDOWS
+ std::string q("\"");
+ std::string qPYTHON(q + PYTHON + q);
+ std::string qscript(q + scriptfile.getName() + q);
+ int rc = _spawnl(_P_WAIT, PYTHON, qPYTHON.c_str(), qscript.c_str(), NULL);
+ if (rc == -1)
+ {
+ char buffer[256];
+ strerror_s(buffer, errno); // C++ can infer the buffer size! :-O
+ ensure(STRINGIZE("Couldn't run Python " << desc << "script: " << buffer), false);
+ }
+ else
+ {
+ ensure_equals(STRINGIZE(desc << " script terminated with rc " << rc), rc, expect);
+ }
+
+#else // LL_DARWIN, LL_LINUX
+ LLProcessLauncher py;
+ py.setExecutable(PYTHON);
+ py.addArgument(scriptfile.getName());
+ ensure_equals(STRINGIZE("Couldn't launch " << desc << " script"), py.launch(), 0);
+ // Implementing timeout would mean messing with alarm() and
+ // catching SIGALRM... later maybe...
+ int status(0);
+ if (waitpid(py.getProcessID(), &status, 0) == -1)
+ {
+ int waitpid_errno(errno);
+ ensure_equals(STRINGIZE("Couldn't retrieve rc from " << desc << " script: "
+ "waitpid() errno " << waitpid_errno),
+ waitpid_errno, ECHILD);
+ }
+ else
+ {
+ if (WIFEXITED(status))
+ {
+ int rc(WEXITSTATUS(status));
+ ensure_equals(STRINGIZE(desc << " script terminated with rc " << rc),
+ rc, expect);
+ }
+ else if (WIFSIGNALED(status))
+ {
+ ensure(STRINGIZE(desc << " script terminated by signal " << WTERMSIG(status)),
+ false);
+ }
+ else
+ {
+ ensure(STRINGIZE(desc << " script produced impossible status " << status),
+ false);
+ }
+ }
+#endif
+ }
+ };
+
+ typedef tut::test_group<TestPythonCompatible> TestPythonCompatibleGroup;
+ typedef TestPythonCompatibleGroup::object TestPythonCompatibleObject;
+ TestPythonCompatibleGroup pycompat("LLSD serialize Python compatibility");
+
+ template<> template<>
+ void TestPythonCompatibleObject::test<1>()
+ {
+ set_test_name("verify python()");
+ python("hello",
+ "import sys\n"
+ "sys.exit(17)\n",
+ 17); // expect nonzero rc
+ }
+
+ template<> template<>
+ void TestPythonCompatibleObject::test<2>()
+ {
+ set_test_name("verify NamedTempFile");
+ python("platform",
+ "import sys\n"
+ "print 'Running on', sys.platform\n");
+ }
+
+ template<> template<>
+ void TestPythonCompatibleObject::test<3>()
+ {
+ set_test_name("verify sequence to Python");
+
+ LLSD cdata(LLSDArray(17)(3.14)
+ ("This string\n"
+ "has several\n"
+ "lines."));
+
+ const char pydata[] =
+ "def verify(iterable):\n"
+ " it = iter(iterable)\n"
+ " assert it.next() == 17\n"
+ " assert abs(it.next() - 3.14) < 0.01\n"
+ " assert it.next() == '''\\\n"
+ "This string\n"
+ "has several\n"
+ "lines.'''\n"
+ " try:\n"
+ " it.next()\n"
+ " except StopIteration:\n"
+ " pass\n"
+ " else:\n"
+ " assert False, 'Too many data items'\n";
+
+ // Create a something.llsd file containing 'data' serialized to
+ // notation. It's important to separate with newlines because Python's
+ // llsd module doesn't support parsing from a file stream, only from a
+ // string, so we have to know how much of the file to read into a
+ // string.
+ NamedTempFile file(".llsd",
+ // NamedTempFile's boost::function constructor
+ // takes a callable. To this callable it passes the
+ // std::ostream with which it's writing the
+ // NamedTempFile. This lambda-based expression
+ // first calls LLSD::Serialize() with that ostream,
+ // then streams a newline to it, etc.
+ (lambda::bind(LLSDSerialize::toNotation, cdata[0], lambda::_1),
+ lambda::_1 << '\n',
+ lambda::bind(LLSDSerialize::toNotation, cdata[1], lambda::_1),
+ lambda::_1 << '\n',
+ lambda::bind(LLSDSerialize::toNotation, cdata[2], lambda::_1),
+ lambda::_1 << '\n'));
+
+ python("read C++ notation",
+ lambda::_1 <<
+ import_llsd <<
+ "def parse_each(iterable):\n"
+ " for item in iterable:\n"
+ " yield llsd.parse(item)\n" <<
+ pydata <<
+ // Don't forget raw-string syntax for Windows pathnames.
+ "verify(parse_each(open(r'" << file.getName() << "')))\n");
+ }
+
+ template<> template<>
+ void TestPythonCompatibleObject::test<4>()
+ {
+ set_test_name("verify sequence from Python");
+
+ // Create an empty data file. This is just a placeholder for our
+ // script to write into. Create it to establish a unique name that
+ // we know.
+ NamedTempFile file(".llsd", "");
+
+ python("write Python notation",
+ lambda::_1 <<
+ "from __future__ import with_statement\n" <<
+ import_llsd <<
+ "DATA = [\n"
+ " 17,\n"
+ " 3.14,\n"
+ " '''\\\n"
+ "This string\n"
+ "has several\n"
+ "lines.''',\n"
+ "]\n"
+ // Don't forget raw-string syntax for Windows pathnames.
+ // N.B. Using 'print' implicitly adds newlines.
+ "with open(r'" << file.getName() << "', 'w') as f:\n"
+ " for item in DATA:\n"
+ " print >>f, llsd.format_notation(item)\n");
+
+ std::ifstream inf(file.getName().c_str());
+ LLSD item;
+ // Notice that we're not doing anything special to parse out the
+ // newlines: LLSDSerialize::fromNotation ignores them. While it would
+ // seem they're not strictly necessary, going in this direction, we
+ // want to ensure that notation-separated-by-newlines works in both
+ // directions -- since in practice, a given file might be read by
+ // either language.
+ ensure_equals("Failed to read LLSD::Integer from Python",
+ LLSDSerialize::fromNotation(item, inf, LLSDSerialize::SIZE_UNLIMITED),
+ 1);
+ ensure_equals(item.asInteger(), 17);
+ ensure_equals("Failed to read LLSD::Real from Python",
+ LLSDSerialize::fromNotation(item, inf, LLSDSerialize::SIZE_UNLIMITED),
+ 1);
+ ensure_approximately_equals("Bad LLSD::Real value from Python",
+ item.asReal(), 3.14, 7); // 7 bits ~= 0.01
+ ensure_equals("Failed to read LLSD::String from Python",
+ LLSDSerialize::fromNotation(item, inf, LLSDSerialize::SIZE_UNLIMITED),
+ 1);
+ ensure_equals(item.asString(),
+ "This string\n"
+ "has several\n"
+ "lines.");
+ }
+}
diff --git a/indra/llcommon/tests/setpython.py b/indra/llcommon/tests/setpython.py
new file mode 100644
index 0000000000..df7b90428e
--- /dev/null
+++ b/indra/llcommon/tests/setpython.py
@@ -0,0 +1,19 @@
+#!/usr/bin/python
+"""\
+@file setpython.py
+@author Nat Goodspeed
+@date 2011-07-13
+@brief Set PYTHON environment variable for tests that care.
+
+$LicenseInfo:firstyear=2011&license=viewerlgpl$
+Copyright (c) 2011, Linden Research, Inc.
+$/LicenseInfo$
+"""
+
+import os
+import sys
+import subprocess
+
+if __name__ == "__main__":
+ os.environ["PYTHON"] = sys.executable
+ sys.exit(subprocess.call(sys.argv[1:]))